参照网上代码获取经纬度,始终不能正确执行。
经过不停的调试,最后终于可以获取经纬度了,在此先记录下来。
private double mLongitude;
private double mLatitude;
public String getLocation(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(true);
String bestProvider = locationManager.getBestProvider(criteria, true);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Location location = locationManager
.getLastKnownLocation(bestProvider);
if (location != null) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
} else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
if (location != null ) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}else {
mLatitude = 39.869411;
mLongitude = 116.383337;
}
}
});
}
}
return mLatitude+","+mLongitude;
配置文件
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
下面是通过高德方法获取经纬度:
private LocationManagerProxy mLocationManagerProxy;
mLocationManagerProxy = LocationManagerProxy.getInstance(v.getContext());
mLocationManagerProxy.setGpsEnable(false);
mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork,
-1, 1500, new AMapLocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onLocationChanged(AMapLocation arg0) {
if (arg0 != null
&& arg0.getAMapException()
.getErrorCode() == 0) {
Double geoLat = arg0.getLatitude();
Double geoLng = arg0.getLongitude();
// loadData(geoLat+","+geoLng);
}
}
});
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="fe11c2c214e25629193d1adca84b1a38" />
需要到官方网站上申请value值
下面的方法有时候不能获取地理坐标:(HTC S720无法获取地理坐标)
public static Location getNetworkLocation(Context context){
LocationManager mLocationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null){
location = new Location("TianAnMen");
location.setLatitude(39.915);
location.setLongitude(116.404);
}
return location;
}