好的,我花了很多时间搜索和阅读,而不仅仅是这个网站.大多数线程处理GPS或试图让它在模拟器中工作.developer.android.com上的文档只解释了一点.我不能做一件简单的工作.像大多数有这个问题的人一样,我只想从网络提供商那里得到最后一个已知的位置,即使我使用谷歌地图(使用网络提供商,因为GPS被关闭),它也不会得到它,它显示我距离酒店仅1/8到1/4英里.好吧,所以"提供者"(手机上的手机信号塔/ Wi-Fi)正在修复Google地图.我尝试我的应用程序,无法获取最后的已知位置.我甚至没有尝试在地图中显示它,我按下按钮以获取位置后,它将lat和长坐标打印到屏幕上.文档没有说明必须从同一个活动做出的最后一个位置.它说它将返回指定提供者的最后一个已知位置.我正在指定NETWORK_PROVIDER,文档中说的是单元塔和Wi-Fi.(是的,我已经清楚了.)
有谁知道这是如何工作的?即使在谷歌网站上,我还没有看到一个很好的解释.如果你不能说,我很沮丧.这显然是我正在做的事情(或者不是),所以我不会责怪任何人/除了我自己,除了文件对我的需求有点太短暂.
更新:我现在正在使用位置监听器并获取位置,我假设最后一个位置.能够在没有通过位置监听器开始位置更新的情况下获取最后一个位置似乎是合乎逻辑的,但我不是,现在我正在使用位置监听器.
现在我的问题是我无法获得改变的位置.我的代码现在没有做任何事情,除了尝试通过监听器获取位置更新,并且还接受按下按钮以抢夺最后的已知位置.Google地图会告诉我我的位置,但我会在20英里外的地方找到我的代码.我有一种感觉,我实际上可能会获得一个新的位置,但是我可能错误地使用了final/static声明,这使我无法在需要时存储和使用该位置.我的清单具有internet和fine_location权限,活动的xml工作正常 - 它只提供一个按钮来打开/关闭实时搜索位置,一个用于手动检索lastknownlocation的按钮,以及一个用于显示纬度和经度的textview.我试图去掉非必需品以防止这种情况持续太长时间.
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) {}
};
Run Code Online (Sandbox Code Playgroud)
}
尝试检查一下......一旦你onLocationChange被调用,之后你可以使用上一个已知位置得到的旧值.
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
old_latitude=location.getLatitude();
old_longitude=location.getLongitude();
Log.d("old","lat : "+old_latitude);
Log.d("old","long : "+old_longitude);
this.onLocationChanged(location);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12598 次 |
| 最近记录: |