Created
April 9, 2013 16:31
-
-
Save saxman/5347195 to your computer and use it in GitHub Desktop.
Plotting markers on a map in Android using data from a JSON web service and the Google Maps Android API v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* 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. | |
*/ | |
package com.example.google.maps.demo; | |
import com.google.android.gms.maps.GoogleMap; | |
import com.google.android.gms.maps.SupportMapFragment; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.util.Log; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
/** | |
* @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; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setUpMapIfNeeded(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
setUpMapIfNeeded(); | |
} | |
private void setUpMapIfNeeded() { | |
if (map == null) { | |
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) | |
.getMap(); | |
if (map != null) { | |
setUpMap(); | |
} | |
} | |
} | |
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); | |
map.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) | |
)) | |
); | |
} | |
} | |
} |
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)
@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
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