android中打开地理位置,Android中获取地理位置经纬度

一种简单的获取经纬度的方法, 室内可能获取不到。需要打开GPS。

/**

* 获取用户地理位置,未获取到为默认显示 0.0#0.0

* 需要权限ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION,INTERNET

*

* @param context

* @return 纬度#经度

*/

public static String getUserLocation(Context context) {

double latitude = 0.0, longitude = 0.0;

String locationProvider = null;

// 获取地理位置管理器

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );

// 判断网络是否可用

Boolean isNet = isConnectingToInternet(context);

Log. i(TAG, "isNet" + isNet);

if (isNet) {

locationProvider = LocationManager.NETWORK_PROVIDER ;

} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

// GPS室内会获取不到位置

locationProvider = LocationManager. GPS_PROVIDER;

} else {

locationProvider = LocationManager.PASSIVE_PROVIDER ;

Log. i(TAG, "无法获取当前位置, 请检查网络是否可用和GPS是否开启" );

}

// 获取Location

Location location = locationManager.getLastKnownLocation(locationProvider);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

// 不为空,显示地理位置经纬度

Log. i(TAG, "纬度:" + latitude + "经度:" + longitude);

}

return latitude + "#" + longitude;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值