我正在过滤用户周围的附近地方,我希望得到用户周围每个标记/地点的place_id。
但是它只显示所有标记/位置的1个place_id。 logcat显示了不同的place_id但它是应用程序,它只是片段1 place_id。
为了更深入的理解:我发布了一个示例视频:https://www.youtube.com/watch?v=-0pmjblb_G8。
正如您所看到的,每次我尝试点击标记/地点时,它都会显示与另一个标记/位置相同的place_id。
我可以发布任何其他所需的代码以便澄清。
更新:其他信息:
我将placeId设为静态字符串placeId。 然后我在MainActivity上添加了这个
public boolean onMarkerClick(Marker marker) {
Toast.makeText(traffic.this, " " + GetNearbyPlacesData.placeId, Toast.LENGTH_SHORT).show();
return true;
}
这样我就可以看到每个标记/位置的placeId。
更新:其他信息:
public class GetNearbyPlacesData extends AsyncTask<Object, String, String> {
String googlePlacesData;
GoogleMap mMap;
String url;
static String placeID = "";
@Override
protected String doInBackground(Object... params) {
try {
Log.d("GetNearbyPlacesData", "doInBackground entered");
mMap = (GoogleMap) params[0];
url = (String) params[1];
DownloadUrl downloadUrl = new DownloadUrl();
googlePlacesData = downloadUrl.readUrl(url);
Log.d("GooglePlacesReadTask", "doInBackground Exit");
} catch (Exception e) {
Log.d("GooglePlacesReadTask", e.toString());
}
return googlePlacesData;
}
@Override
protected void onPostExecute(String result) {
Log.d("GooglePlacesReadTask", "onPostExecute Entered");
List<HashMap<String, String>> nearbyPlacesList = null;
DataParser dataParser = new DataParser();
nearbyPlacesList = dataParser.parse(result);
ShowNearbyPlaces(nearbyPlacesList, result);
Log.d("GooglePlacesReadTask", "onPostExecute Exit");
}
private void ShowNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList, String result) {
for (int i = 0; i < nearbyPlacesList.size(); i++) {
Log.d("onPostExecute", "Entered into showing locations");
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googlePlace = nearbyPlacesList.get(i);
double lat = Double.parseDouble(googlePlace.get("lat"));
double lng = Double.parseDouble(googlePlace.get("lng"));
String placeName = googlePlace.get("place_name");
String vicinity = googlePlace.get("vicinity");
try {
JSONObject jsonObject = new JSONObject(result);
//CHECK IF i IS CHANGING HERE
//CHECK IF RESULTS AND PLACE ID ARE CHANGING WITH DEBUGGER
placeID = jsonObject.getJSONArray("results").getJSONObject(i).getString("place_id");
Log.d("onPostExecute", placeID);
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
Marker m = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.snippet("placeId:" + placeID)
.title("placeId:" + placeID));
m.showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
} catch (Exception e) {
}
// markerOptions.title(placeName + " : " + vicinity);
//move map camera
}
}
}
答案 0 :(得分:0)
地方ID会返回到Places API附近搜索的JSON响应中。
你可以看到类似的内容:
...
"results":[
{
"geometry":{
"location":{
"lat":-33.8688197,"lng":151.2092955
},
"viewport":{
"northeast":{
"lat":-33.5782519,"lng":151.3429976
},
"southwest":{
"lat":-34.118328,"lng":150.5209286
}
}
},
"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id":"044785c67d3ee62545861361f8173af6c02f4fae",
"name":"Sydney",
"photos":[
{
"height":2448,
"html_attributions":[
"Hatim Ghadhban Abood"
],
"photo_reference":"CoQBdwAAAMIVPsN50AxLsKj-QiQ2pCrKaXTZd-OpHHcQ4BrHkrFYylWZS1MP0GDqAA8GrZIsqvgg97eDsQVV7UPGeGnIvRTJHPPbnkOmW6VhoxKXAKX_0NqCYAiK4twz4a4ELPwPC0tyCeU1KtkYcKknhhgohG41F4JbFru4m0NiFE3TtAp_EhC1wpA1LN68NZOJ1l_iXctoGhTNz_68e8x-dPM6G0PAZiWezSkvsg",
"width":3264
}
],
"place_id":"ChIJP3Sa8ziYEmsRUKgyFmh9AQM",
"reference":"CmRbAAAA5LVfsLJXuBE1N7KDgJUTSgHHbGpGUVGgnLPVkobaylk6n9MsiPlqlYMmC0AdkqgzITScK_qDs05-n3ut6RMcrT2rHRwN36_pI8RoPNbNe9fPbOIyO2kTX9o7svURRqTpEhB4Phdre1afknnC5Ex2AewpGhRNNL5e4kxo1_L4BJgGZrRZlmWnmQ",
"scope":"GOOGLE",
"types":[
"colloquial_area","locality","political"
],
"vicinity":"Sydney"
},...
您需要提取一个字段place_id
。如果您按照上面提到的教程,您可以看到您在DataParser类中解析JSON响应,因此您必须修改private HashMap<String, String> getPlace(JSONObject googlePlaceJson)
方法以将地点ID添加到地点HashMap:
private HashMap<String, String> getPlace(JSONObject googlePlaceJson) {
HashMap<String, String> googlePlaceMap = new HashMap<String, String>();
String placeName = "-NA-";
String vicinity = "-NA-";
String latitude = "";
String longitude = "";
String reference = "";
String placeID = "";
Log.d("getPlace", "Entered");
try {
if (!googlePlaceJson.isNull("name")) {
placeName = googlePlaceJson.getString("name");
}
if (!googlePlaceJson.isNull("vicinity")) {
vicinity = googlePlaceJson.getString("vicinity");
}
latitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
longitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
reference = googlePlaceJson.getString("reference");
//Let's get place ID here
placeID = googlePlaceJson.getString("place_id");
googlePlaceMap.put("place_name", placeName);
googlePlaceMap.put("vicinity", vicinity);
googlePlaceMap.put("lat", latitude);
googlePlaceMap.put("lng", longitude);
googlePlaceMap.put("reference", reference);
//Let's add place ID to hash map
googlePlaceMap.put("place_id", placeID);
Log.d("getPlace", "Putting Places");
} catch (JSONException e) {
Log.d("getPlace", "Error");
e.printStackTrace();
}
return googlePlaceMap;
}
之后,您将能够在异步任务中检索显示标记的地点ID(查看ShowNearbyPlaces
方法):
public class GetNearbyPlacesData extends AsyncTask<Object, String, String> {
String googlePlacesData;
GoogleMap mMap;
String url;
@Override
protected String doInBackground(Object... params) {
try {
Log.d("GetNearbyPlacesData", "doInBackground entered");
mMap = (GoogleMap) params[0];
url = (String) params[1];
DownloadUrl downloadUrl = new DownloadUrl();
googlePlacesData = downloadUrl.readUrl(url);
Log.d("GooglePlacesReadTask", "doInBackground Exit");
} catch (Exception e) {
Log.d("GooglePlacesReadTask", e.toString());
}
return googlePlacesData;
}
@Override
protected void onPostExecute(String result) {
Log.d("GooglePlacesReadTask", "onPostExecute Entered");
List<HashMap<String, String>> nearbyPlacesList = null;
DataParser dataParser = new DataParser();
nearbyPlacesList = dataParser.parse(result);
ShowNearbyPlaces(nearbyPlacesList);
Log.d("GooglePlacesReadTask", "onPostExecute Exit");
}
private void ShowNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList) {
for (int i = 0; i < nearbyPlacesList.size(); i++) {
Log.d("onPostExecute","Entered into showing locations");
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googlePlace = nearbyPlacesList.get(i);
double lat = Double.parseDouble(googlePlace.get("lat"));
double lng = Double.parseDouble(googlePlace.get("lng"));
String placeName = googlePlace.get("place_name");
String vicinity = googlePlace.get("vicinity");
//Retrieve a place ID here
String placeID = googlePlace.get("place_id");
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName + " : " + vicinity);
mMap.addMarker(markerOptions);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
}
}