我想要实现某些我虽然很简单但现在最终变得有点困惑的东西。 我想以这种方式获得用户的坐标从GPS提供商明确获取坐标
- 是否有GPS提供商?
- 如果是的话,得到的GPS
- 否则坐标,如果有一个网络提供商从那里
让他们我同时使用locationManager
和onLocationChange()
,并与locationClient
新的API,但我无法从我的设备的GPS获取坐标,只能从网络获取。
这是一个与locationManager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use default
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
// new AlertDialog.Builder(v.getContext()).setMessage("Provider " + provider + " has been selected.").show();
onLocationChanged(location);
System.out.println(location.getLatitude());
Intent intent = new Intent(getApplicationContext(),DriveMeActivity.class);
intent.putExtra("monument", monument);
intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
startActivity(intent);
} else {
new AlertDialog.Builder(con).setMessage("Location not available").show();
System.out.println("Location not available");
System.out.println("Location not available");
}
,这是一个与locationClient
GooglePlayServicesUtil.isGooglePlayServicesAvailable(v.getContext())
if(mLocationClient.getLastLocation()!=null)
Toast.makeText(getApplicationContext(),
mLocationClient.getLastLocation().getLongitude()+ "|" +
mLocationClient.getLastLocation().getLatitude() ,
Toast.LENGTH_SHORT).show();
您需要展示更多代码。不知何故,即使您认为它应该是“是”,您的“有没有GPS提供商”的请求也会变得“不”。也许你的应用程序不允许访问定位服务(隐私?)。 – Floris
好吧,我现在将发布代码。我在清单中拥有所有必要的权限,如果有可用的gps提供程序,我也检查过它,但它返回的位置为空 – Libathos
http://stackoverflow.com/a/16393534/2345913 – CRUSADER