我需要在谷歌地图上显示json数据。
我的问题是它只在谷歌地图上加载了一个json数组,而另一个则没有加载。当我关闭应用程序并再次打开时,它也没有显示任何标记。这是我的代码
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
here i'm loading service call.
private void setUpMap() {
// Retrieve the city data from the web service
// In a worker thread since it's a network operation.
new Thread(new Runnable() {
public void run() {
try {
retrieveAndAddCities();
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot retrive cities", e);
return;
}
}
}).start();
}
protected void retrieveAndAddCities() throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
throw new IOException("Error connecting to service", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
// Create markers for the city data.
// Must run this on the UI thread since it's a UI operation.
runOnUiThread(new Runnable() {
public void run() {
try {
createMarkersFromJson(json.toString());
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
});
}
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);
System.out.println("ben:"+jsonObj);
mMap.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.snippet(Integer.toString(jsonObj.getInt("population")))
.position(new LatLng(
jsonObj.getJSONArray("latlng").getDouble(0),
jsonObj.getJSONArray("latlng").getDouble(1)
))
);
}
1s when i load google maps its loading only one location from json array. when i close the app and open again it will not load any location.
i have multiple location from json service but it load only one location on google maps. dont know what is issues.
I am using this example as a reference.
https://gist.github.com/TimPim/5902100
This my service
[ { "name": "John Doe", "latlng": [ 62.457434, 17.349869 ], "population": "123" }, { "name": "Jane Doe", "latlng": [ 62.455102, 17.345599 ], "population": "132" }, { "name": "James Bond", "latlng": [ 62.458287, 17.356306 ], "population": "123" } ]
任何人都可以提供示例代码。如果我从后台更改此服务,则不会在谷歌地图上显示任何内容。