google-app-enginegoogle-mapsgwtgoogle-visualizationgwt-rpc

How to get latitude/longitude from a city name for GWT's MapVisualization?


Is there a possibility to get the Google Maps's latitude and longitude code from just a city name or a street name?

For example: I just want the latitude and longitude code of Paris (France), so I can use it for the Google's MapVisualization (Google Web Toolkit, Google App Engine and RPC).

Because it's very inconvenient to find the latitude and longitude code every time for each city.

package com.practicum.client.out;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.MapVisualization;
import com.google.gwt.visualization.client.visualizations.MapVisualization.Options;


public class DataOutGoogleMap {

public DataOutGoogleMap(Runnable runnable) {
}

public Widget createGoogleMapView() {
    DataTable data = DataTable.create();
    data.addColumn(ColumnType.NUMBER, "Lat");
    data.addColumn(ColumnType.NUMBER, "Lon");
    data.addColumn(ColumnType.STRING, "Name");
    data.addRows(3);

    data.setValue(0, 0, 52.37022);
    data.setValue(0, 1, 4.89517);
    data.setValue(0, 2, "This is Amsterdam");

    data.setValue(1, 0, 51.50813);
    data.setValue(1, 1, -0.12801);
    data.setValue(1, 2, "This is London");

    data.setValue(2, 0, "Paris, France"); // DONT WORK?
    data.setValue(2, 1, "");
    data.setValue(2, 2, "This is Paris");

    Options options = Options.create();
    options.setEnableScrollWheel(true);
    options.setMapType(MapVisualization.Type.NORMAL);
    options.setShowTip(true);

    return new MapVisualization(data, options, "400px", "300px");
    }
}

Solution

  • This can be accomplished with the Google Geocoding API. For example, to find the lat and lng for Paris, you could submit

    GET
    http://maps.googleapis.com/maps/api/geocode/json?address=Paris,+France&sensor=false
    

    and the desired lat and lng are contained in the JSON response at a key called "location" inside the a "geometry" dictionary:

     "geometry" : {
                ...
                "location" : {
                   "lat" : 48.8566140,
                   "lng" : 2.35222190
                },