android google map v2 获取当前位置,如何在google map v2中获取当前位置信息?

代码来获得用户

public void getMyLocation(Activity activity) {

int requestPermissionsCode = 50;

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

if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, requestPermissionsCode);

return;

}

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

if (locationListener != null)

locationListener.onLocationFound(latLng);

return;

}

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new SingleUpdateListener(), null);

}

此代码的位置,从经纬度

public Address getLocationFromLatLng(Context context, LatLng latLng) {

android.location.Geocoder geoCoder = new Geocoder(context, Locale.getDefault());

try {

return geoCoder.getFromLocation(latLng.latitude, latLng.longitude, 1).get(0);

} catch (IOException e) {

return null;

}

}

获得地址,你必须在谷歌API控制台 注册该应用程序的代码for Android 6.0 plus pay attation to the listeners .. 这是一个工作代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值