gpt4 book ai didi

android - 在没有GPS的情况下定位android中的当前位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:12 25 4
gpt4 key购买 nike

我想开发一个应用程序,如果我打开该应用程序,它将显示我设备的当前位置。我想在不使用 gps 的情况下进行。我已经为它写了一个代码。但它会在打开 map 时显示美国的位置。我希望它显示我当前的位置(即印度浦那)。我如何获得大致正确的位置?

这是我的代码

protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.main);

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener locListener = new LocationListener(){
public void onLocationChanged(Location loc)
{

loc.getLatitude();
loc.getLongitude();

String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();

Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}

最佳答案

这是 MyLocation 监听器,无需 GPS,使用网络提供商即可获取当前位置

public class MyLocationListener extends Service implements LocationListener {

private static final String TAG = "MyLocationListener";

private Context context = null;
private Location location = null;
private LocationManager locationManager = null;

boolean isNetworkEnabled = false;
boolean canGetLocation = false;

public double latitude = 0.0;
public double longitude = 0.0;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

public MyLocationListener(Context ctx) {
Log.v(TAG + ".MyLocationListener",
"MyLocationListener constructor called");
this.context = ctx;
getLocationValue();
}

public Location getLocationValue() {
Log.v(TAG + ".getLocationValue", "getLocationValue method called");

try {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);

isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (isNetworkEnabled) {

// Toast.makeText(context, "Net", 1).show();
Log.v(TAG + ".getLocationValue", "Network provider enabled");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.v(TAG, "Co-ordinates are: " + latitude + " "+ longitude);

}
}

} else {
showSettingsAlert();
}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(MyLocationListener.this);
}
}

public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}

public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}

/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}

/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

alertDialog.setTitle("GPS Settings");
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");

alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

}

只要需要当前位置坐标,就调用上面的代码

MyLocationListener mylistner = new MyLocationListener(context);
double lat = mylistner.latitude;
double lon = mylistner.longitude;

关于android - 在没有GPS的情况下定位android中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com