如何在地图上显示多个标记
How to show the multiple markers on maps
谁能解释如何使用以下 JSON 代码在 android 中的 google 地图上显示多个标记?
[
{"id":"1","lat":"18.105917","lng":"78.838325","location":" Laxmi Filling Station( Medak Road)","distance":"0"},
{"id":"3","lat":"18.105952","lng":"78.840366","location":"Fathima Filling Station (Medak Road)","distance":"0.1340667689594937"},
{"id":"13","lat":"18.093713","lng":"78.843775","location":"Thirumala Thirupathi Filling Station (Ensanpally Road)","distance":"0.9160924683870764"},
{"id":"12","lat":"18.101497","lng":"78.852353","location":"MS Balaji Filling Station ( Old Busstand)","distance":"0.9706182480093937"}
]
所有错误都已解决,地图上仍未显示标记
private void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
String location=jsonObj.getString("location");
double lat=jsonObj.getDouble("lat");
double lng=jsonObj.getDouble("lng");
String id=jsonObj.getString("id");
Log.e("detailes", lat+" "+lng+" "+location);
map.addMarker(new MarkerOptions()
.title(location)
.snippet(id)
.position(new LatLng(lat, lng))
);
}
}
- 你为什么打电话给
jsonObj.getInt("population")
?它不存在
在你的 API 中。
- 您患有
Type mismatches on lookups
.
请像这样更改您的代码。
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
String _id =jsonObject.getString("id");
String _lat =jsonObject.getString("lat");
String _lng =jsonObject.getString("lng");
map.addMarker(new MarkerOptions()
.title(jsonObject.getString("name"))
.snippet(Integer.toString(jsonObject.getString("distance")))
.position(new LatLng(_lat,_lng )); // If problem then convert String to double. Use getDouble() instead of getString()
}
填充键不存在于 json 字符串中
private void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
map.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.snippet(jsonObj.getString("location"))
.position(new LatLng(
jsonObj.getDouble("lat"),
jsonObj.getDouble("lng")
))
);
}
}
谁能解释如何使用以下 JSON 代码在 android 中的 google 地图上显示多个标记?
[
{"id":"1","lat":"18.105917","lng":"78.838325","location":" Laxmi Filling Station( Medak Road)","distance":"0"},
{"id":"3","lat":"18.105952","lng":"78.840366","location":"Fathima Filling Station (Medak Road)","distance":"0.1340667689594937"},
{"id":"13","lat":"18.093713","lng":"78.843775","location":"Thirumala Thirupathi Filling Station (Ensanpally Road)","distance":"0.9160924683870764"},
{"id":"12","lat":"18.101497","lng":"78.852353","location":"MS Balaji Filling Station ( Old Busstand)","distance":"0.9706182480093937"}
]
所有错误都已解决,地图上仍未显示标记
private void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
String location=jsonObj.getString("location");
double lat=jsonObj.getDouble("lat");
double lng=jsonObj.getDouble("lng");
String id=jsonObj.getString("id");
Log.e("detailes", lat+" "+lng+" "+location);
map.addMarker(new MarkerOptions()
.title(location)
.snippet(id)
.position(new LatLng(lat, lng))
);
}
}
- 你为什么打电话给
jsonObj.getInt("population")
?它不存在 在你的 API 中。 - 您患有
Type mismatches on lookups
.
请像这样更改您的代码。
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
String _id =jsonObject.getString("id");
String _lat =jsonObject.getString("lat");
String _lng =jsonObject.getString("lng");
map.addMarker(new MarkerOptions()
.title(jsonObject.getString("name"))
.snippet(Integer.toString(jsonObject.getString("distance")))
.position(new LatLng(_lat,_lng )); // If problem then convert String to double. Use getDouble() instead of getString()
}
填充键不存在于 json 字符串中
private void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
map.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.snippet(jsonObj.getString("location"))
.position(new LatLng(
jsonObj.getDouble("lat"),
jsonObj.getDouble("lng")
))
);
}
}