如何在不使用谷歌地图和其他地图的情况下获取用户当前位置?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在不使用谷歌地图和其他地图的情况下获取用户当前位置?相关的知识,希望对你有一定的参考价值。
我正在开发天气应用程序,当应用程序首次启动时,我想检测用户当前位置,但我尝试使用谷歌地图,但他们要求信用卡,以获得谷歌地图的API密钥,不幸的是,我没有信用卡。你有任何其他解决方案或替代品吗?
答案
在OnCreate中调用此函数
private void location() {
Utils.loge(TAG, "location");
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Constants.location = location;
Utils.loge(TAG, "onLocationChanged : " + location.getLatitude() + ", " + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location updates
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
}
在你的清单中:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
以上是关于如何在不使用谷歌地图和其他地图的情况下获取用户当前位置?的主要内容,如果未能解决你的问题,请参考以下文章