Android开发模板------ 手机获取本地经纬度

参照网上代码获取经纬度,始终不能正确执行。

经过不停的调试,最后终于可以获取经纬度了,在此先记录下来。

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;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值