我想通过gps知道用户的街道名称或地址,所以我可以在那里放置半径,例如100米或长,并显示特定区域商店,食品场所等..!任何人都可以告诉我
答案 0 :(得分:0)
获取用户位置的代码
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);
}
此代码从LatLng获取地址
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;
}
}
您必须在Google API控制台上注册该应用程序 该代码适用于Android 6.0 加上对听众的敬意 这是一个有效的代码。