/* * 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) )) ); } } }