如何在Android原生模式下获取经度、纬度和城市信息?


浏览:414 次

//2.获取位置提供商、GPS或网络

Listproviders=locationManager.getProviders(true);

if(PROVIDER.contains(LocationManager.GPS_PROVIDER)){

//如果GPS

locationProvider=LocationManager。GPS_提供者;

日志V(“标签”,“定位模式GPS”);

}否则如果(PROVIDER.contains(LocationManager.NETWORK_PROVIDER)){

//如果是网络

locationProvider=LocationManager。网络提供商;

日志V(“标签”,“定位模式网络”);

}其他{

干杯makeText(此,“没有可用的位置提供程序”,Toast.LNGTH_SHORT)。show();

回来

}

如果(Build.VERSION.SDK_INT>=版本.VERSION_CODES.M){

//获取权限(如果没有打开权限,将弹出对话框询问是否打开权限)

if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)

!= PackageManager。许可_授予||

活动公司。checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)

!= PackageManager。许可(授予){

//请求权限

活动公司。requestPermissions(这是新的String[]{Manifest.permission.ACCESS_FINE_LOCATION,

许可证清单。ACCESS_粗糙_位置}、位置_代码);

}其他{

//3.获取最后一个位置。第一次运行时此值为空

位置位置=locationManager.getLastKnownLocation(locationProvider);

if(位置!=空){

干杯makeText(this,location.getLongitude()+“”+

地方getLatitude()+“”,Toast。长度_短).show();

日志V(“TAG”,“获取最后一个位置-经度和纬度:”+location.getLongitude()+“+”location.getLatitude());

getAddress(位置);

}其他{

//监控地理位置的变化。第二和第三个参数分别是更新的最小时间minTime和最小距离minDistance

locationManager。requestLocationUpdates(locationProvider,3000,1,locationListener);

}

}

}其他{

位置位置=locationManager.getLastKnownLocation(locationProvider);

if(位置!=空){

干杯makeText(this,location.getLongitude()+“”+

地方getLatitude()+“”,Toast。长度_短).show();

日志V(“TAG”,“获取最后一个位置-经度和纬度:”+location.getLongitude()+“+”location.getLatitude());

getAddress(位置);

}其他{

//监控地理位置的变化。第二和第三个参数分别是更新的最小时间minTime和最小距离minDistance

locationManager。requestLocationUpdates(locationProvider,3000,1,locationListener);

}

}

}

public LocationListener LocationListener=新LocationListeners(){

//当提供商状态在可用、暂时不可用和无服务之间直接切换时,会触发此功能

@覆盖

public void onStatusChanged(字符串提供程序,int状态,捆绑附加){

}

//当提供商启用时(如打开GPS时),会触发此功能

@覆盖

public void onProviderEnabled(字符串提供程序){

}

//当提供商被禁用时,例如,GPS被关闭时,会触发此功能

@覆盖

public void onProviderDisabled(字符串提供程序){

}

//当坐标改变时,会触发此功能。如果提供程序传递相同的坐标,则不会触发它

@覆盖

public void onLocationChanged(位置位置){

if(位置!=空){

//如果位置发生变化,请重新显示地理位置经度和纬度

Toast.makeText(TestLocationActivity.this,location.getLongitude()+“”+

地方getLatitude()+“”,Toast。长度_短).show();

日志V(“TAG”,“监测地理位置变化-经度和纬度:”+location.getLongitude()+“+”location.getLatitude());

}

}

};

@覆盖

public void onRequestPermissionsResult(int requestCode,@NonNull String[]权限,@NonNull int[]grantResults){

开关(requestCode){

案例位置_代码:

如果(grantResults.length>0&&grantResuts[0]==

相关资讯:
什么是新订单系统

什么是新订单系统

查看
农产品分销软件如何帮助农产品分销

农产品分销软件如何帮助农产品分销

查看
如何成为一名程序员?这是重要的几点

如何成为一名程序员?这是重要的几点

查看
什么是域名归档?我们为什么要归档?备案后,您将获得以下好处!

什么是域名归档?我们为什么要归档?备案后,您将获得以下好处!

查看
上网速度慢。我该怎么办?

上网速度慢。我该怎么办?

查看