我使用以下代码来获取我当前的位置.但我面临的问题是,纬度和经度总是返回0.0.我已打开手机中的GPS设置并设置所有权限.但我仍然面对这个问题.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); customLocationListener = new CustomLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,customLocationListener); class CustomLocationListener implements LocationListener{ ............ public void onLocationChanged(Location argLocation) { if(location != null) { int latitude=(int)(argLocation.getLatitude()*1E6); int longitude=(int)(argLocation.getLongitude()*1E6); } } ........ }
你们中的任何人都知道为什么吗?
注意:我的活动扩展了MapActivity而不是Location Listener
解决方法
试试这种方式
locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Criteria locationCritera = new Criteria(); String providerName = locationManager.getBestProvider(locationCritera,true); if(providerName!=null) location = locationManager.getLastKnownLocation(providerName); locationListener = new MyLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,locationListener);
在mylocationlistener中设置您的位置对象
private class MyLocationListener implements LocationListener { public void onLocationChanged(Location loc) { if (loc != null) { location = loc; } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider,int status,Bundle extras) { } }