【发布时间】:2018-03-17 23:59:45
【问题描述】:
我正在编写一个 Android 应用,用于显示您所在位置附近的披萨店 为此,我使用 URL 调用谷歌地图: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={latitude,longitude}&type=pizza&sensor=true&key={MY_KEY}
我正在通过扩展 AsyncTask 并使用方法 doInBackground 和 onPostExecute 来处理数据。
从我获取和解析的数据中,我得到了地点 id,现在我想再次调用谷歌来获取地点信息电话,评级,开放......(你不会从附近的地方获得这些数据信息)我看到您可以使用以下方式向谷歌发起 URL 调用: https://maps.googleapis.com/maps/api/place/details/json?key={MY_KEY}&placeid={PLACE_ID}
但我不想在 AsyncTask 中调用 AsyncTask。 基本上我想调用第一个 URL 并在解析每个地方时获取扩展信息。
我该怎么做? 我的代码是:
公共类 MapGetNearbyPlacesData 扩展 AsyncTask {
public final static int MAP_INDEX = 0;
public final static int URL_INDEX = 1;
private String googlePlacesData;
private GoogleMap map;
private String url;
@Override
protected String doInBackground(Object... objects) {
this.map = (GoogleMap)objects[MAP_INDEX];
this.url = (String)objects[URL_INDEX];
try {
this.googlePlacesData = MapDownloadURL.readUrl(this.url);
} catch (IOException e) {
e.printStackTrace();
}
return this.googlePlacesData;
}
@Override
protected void onPostExecute(String s) {
List<HashMap<String, String>> nearbyPlaceList;
nearbyPlaceList = MapDataParser.parseNearbyPlaces(s);
showNearbyPlaces(nearbyPlaceList);
}
private void showNearbyPlaces(List<HashMap<String, String>> nearbyPlaceList) {
for(int i = 0; i < nearbyPlaceList.size(); i++) {
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googlePlace = nearbyPlaceList.get(i);
String placeName = googlePlace.get(StringUtils.MAP_PALACE_NAME);
String vicinity = googlePlace.get(StringUtils.MAP_VICINITY);
double lat = Double.parseDouble(googlePlace.get(StringUtils.MAP_LATITUDE));
double lng = Double.parseDouble(googlePlace.get(StringUtils.MAP_LONGITUDE));
String placeId = googlePlace.get(StringUtils.PLACE_ID);
//how can I get the extended place info here?
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
//build all necessary information to display in info window.
String title = new StringBuilder()
.append(placeName).append(StringUtils.infoWindowSplitter)
.append(vicinity).append(StringUtils.infoWindowSplitter)
.append(placeId).toString();
markerOptions.title(title);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
this.map.addMarker(markerOptions);
}
}
}
【问题讨论】:
标签: android google-maps url android-asynctask