好的,我花了很多时间搜索和阅读,而不仅仅是这个网站。大多数线程处理 GPS 或试图让它在模拟器中工作。developer.android.com 上的文档只解释了一点。我不能让一件简单的事情工作。像大多数有这个问题的人一样,我只想从网络提供商那里获得最后一个已知位置,即使在我使用谷歌地图(使用网络提供商,因为 GPS 已关闭)并且它向我显示之后,它也不会得到它距离只有 1/8 到 1/4 英里。好的,所以“提供者”(手机信号塔/wi-fi,如果打开的话)正在修复谷歌地图。我尝试了我的应用程序,但无法获得最后一个已知位置。我什至没有尝试在地图中显示它,我让它在按下按钮以获取位置后将经纬度坐标打印到屏幕上。该文档没有说明必须从同一活动中制作的最后一个位置。它说它将返回指定提供商的最后一个已知位置。我正在指定 NETWORK_PROVIDER,文档中说它是手机信号塔和 wi-fi。(是的,我已经在清单中涵盖了这一点。)
有谁知道这是如何工作的?我还没有看到一个很好的解释,即使在谷歌网站上也是如此。如果你不能告诉我,我很沮丧。这显然是我正在做(或没有)做的事情,所以除了我自己之外,我不会责怪任何人/任何人,除了文档对于我的需要来说太短了。
更新:我现在正在使用位置侦听器并获取位置,我假设是最后一个位置。能够在不通过位置侦听器启动位置更新的情况下获取最后一个位置似乎是合乎逻辑的,但我没有,现在我使用了位置侦听器。
现在我的问题是我无法更改位置。我的代码现在什么都不做,除了尝试通过侦听器获取位置更新,并且还接受按下按钮以获取最后一个已知位置。谷歌地图会告诉我我在哪里,但我一直在我的代码中得到一个 20 英里外的位置。我有一种感觉,我实际上可能会获得一个新位置,但我可能错误地使用了最终/静态声明,这使我无法在需要时存储和使用该位置。我的清单具有 internet 和 fine_location 权限,并且活动的 xml 工作正常 - 它只提供一个按钮来打开/关闭实时搜索位置,一个用于手动检索 lastknownlocation 的按钮,以及一个用于显示纬度和经度的文本视图。
public class RMain extends Activity {
public boolean islivesearchon;
public static double netlistentime = 0 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double netlistendistance = 0 * 1609.344; // miles * conversion to meters
public static double gpslistentime = 30 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double gpslistendistance = 0 * 1609.344; // miles * conversion to meters
private static Location currentlocation;
private LocationManager locationManager;
private LocationListener locationListener;
public static Criteria criteria;
public TextView latview;
public TextView longview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rmain);
latview = (TextView) findViewById(R.id.display_latitude);
longview = (TextView) findViewById(R.id.display_longitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// setup bestProvider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = locationManager.getBestProvider(criteria, true);
// get an initial current location
currentlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
// Define locationListener interface
locationListener = new LocListener();
// React to LiveSearch toggle button press
final Button livesearch = (Button) findViewById(R.id.button_livesearch);
livesearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView livesearchview = (TextView) findViewById(R.id.button_livesearch);
boolean isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(islivesearchon) {
locationManager.removeUpdates(locationListener);
islivesearchon = false;
livesearchview.setText("Live Search is OFF");
}
else {
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isNetEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, (long) netlistentime, (float) netlistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isGpsEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) gpslistentime, (float) gpslistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
}
}
});
// Respond to Get Location button click to get last known location
final Button getlocation = (Button) findViewById(R.id.button_getlocation);
getlocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String bestprovider = locationManager.getBestProvider(criteria, true);
Location lastlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(lastlocation.getLatitude()));
longview.setText(Double.toString(lastlocation.getLongitude()));
}
});
}
private class LocListener implements LocationListener {
@Override
public void onLocationChanged(Location latestlocation) {
// Called when a new location is found by the network location provider.
currentlocation.set(latestlocation);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
}