查找最近的位置在谷歌地图Android版 [英] Find the nearest location in google maps for android
本文介绍了查找最近的位置在谷歌地图Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法找到根据特定的点,谷歌地图最近的位置(经度/纬度)
我读到关于谷歌将API的一些事情,但我不知道如何使用它..
而且是有可能找到下一个最近的一个过???
Is there a way to find the nearest location in Google map according to a specific point ( Long/lat), i read some thing about Google Place API, but i did not realize how to use it.. And is it possible to find the next nearest one too???
推荐答案
下面就是这样,就可以实现IT
HERE IS THE WAY YOU CAN ACHIEVE IT
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
Double l1 = location.getLatitude();
Double l2 = location.getLongitude();
address = GetAddress(l1, l2);
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
private String GetAddress(Double lat, Double lon) {
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
String ret = "";
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(lat, lon, 1);
if (!addresses.equals(null)) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
ret = "Around: " + strReturnedAddress.toString();
} else {
ret = "No Address returned!";
}
} catch (IOException e) {
e.printStackTrace();
ret = "Location: https://maps.google.co.in/maps?hl=en&q=" + lat
+ "," + lon;
} catch (NullPointerException e) {
e.printStackTrace();
ret = lat + "," + lon;
}
return ret;
}
和也AndroidManifest添加这些权限:
And also add these permissions in AndroidManifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
这篇关于查找最近的位置在谷歌地图Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文