Java 寻找附近的地方
我有一个函数Java 寻找附近的地方,java,android,google-maps,geolocation,location,Java,Android,Google Maps,Geolocation,Location,我有一个函数geologite(),用于查找用户输入的位置。我希望此函数能找到用户当前位置附近的位置。有人能帮忙吗 当我调用geologite(“麦当劳”)时,我希望它能找到相对于用户位置最近的麦当劳 public void geoLocate(String searchString) throws IOException { Geocoder gc = new Geocoder(this); List<Address> list = gc.getFromLocati
geologite()
,用于查找用户输入的位置。我希望此函数能找到用户当前位置附近的位置。有人能帮忙吗
当我调用geologite(“麦当劳”)
时,我希望它能找到相对于用户位置最近的麦当劳
public void geoLocate(String searchString) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(searchString, 3);
if (list.size() > 0) {
android.location.Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, "Found: " + locality, Toast.LENGTH_SHORT).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, 17);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions().title(locality).position(new LatLng(lat, lng));
marker = mMap.addMarker(options);
} else {
Toast.makeText(this, "No results found for: " + searchString, Toast.LENGTH_LONG).show();
}
}
检索到当前位置(纬度、经度)后,可以使用的
nearbysearch
搜索McDonnald,如下所示:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=lat,lon&radius=500&type=restaurant&name=mcdonalds&key=YOUR\u API\u key
。实际上,您可以使用StringBuilder
构建此URL,如下所示:
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + type);
googlePlacesUrl.append("&name=mcdonalds");
googlePlacesUrl.append("&key=" + YOUR_GOOGLE_API_KEY);
然后,使用所选的HTTP
客户端向URL发出HTTP
请求,然后处理结果(JSON
)。这将返回与您的搜索匹配的位置,并将包括每个位置的附近区域
我希望这能给你一些关于如何进行的想法。请试一试,如果有帮助请告诉我
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + type);
googlePlacesUrl.append("&name=mcdonalds");
googlePlacesUrl.append("&key=" + YOUR_GOOGLE_API_KEY);