Create a gist now

Instantly share code, notes, and snippets.

Plotting markers on a map in Android using data from a JSON web service and the Google Maps Android API v2
* Copyright (c) 2013 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.util.Log;
* @author saxman
public class MainActivity extends FragmentActivity {
private static final String LOG_TAG = "ExampleApp";
private static final String SERVICE_URL = "YOUR DRIVE SERVICE URL";
protected GoogleMap map;
protected void onCreate(Bundle savedInstanceState) {
protected void onResume() {
private void setUpMapIfNeeded() {
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
if (map != null) {
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 {
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot retrive cities", e);
protected void retrieveAndAddCities() throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
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 = != -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) {
// 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 {
} 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);
map.addMarker(new MarkerOptions()
.position(new LatLng(

You should have added an example.json structure somewhere. Would be awesome :)

@jackbillstrom Look in the comment at the begining. There you have a JSON

jbridge commented Oct 27, 2014

the problem I faced is that:

my json file will be updated hourly, and i read cached json file always.

how can I read updated raw json (ascii) file from GDrive?


For me it only displays the marker of the first 18 objects in the json file. It doesnt display more than 18 markers. How to solve this?

MikaeloN commented Feb 4, 2016

this worked fine for me!!!but i am facing the same problem with jbridge.What if my json is updated hourly??i need to update the markers

Gsamy commented Jul 25, 2016

how to map json data with weblayout response, for eg: if i took firstname from weblayout and mapping the value from text file (file holding jsondata)

kagai commented Oct 20, 2016

@jackbillstrom Hey am working on something Similar to this but am passing a delivery note number in an edittext in another activity that finds a builds a string url that fetches the json data with the coordinates , the problem is that i cant move data from the activity with the edit text to the map in another activity


In line 61 what is .getMap() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment