Получить координаты из Google Maps API - PullRequest
0 голосов
/ 04 июня 2011

У меня есть список адресов, теперь я хочу получить их долготы и широты.Я хотел бы использовать API Google Maps через Java.Как бы я мог получить только один набор советов для одного адреса.(потому что я мог бы легко реализовать для нескольких)

Ответы [ 4 ]

5 голосов
/ 04 июня 2011

Вы также можете использовать Google Geocoder Java API GeoGoogle , например:

// Initialize a new GeoAddressStandardizer-class with your API-Key
GeoAddressStandardizer st = new GeoAddressStandardizer(apikey);
// Get a list of possible matching addresses
List<GeoAddress> addresses = st.standardizeToGeoAddresses(address);
// Get the first address (like you posted above)
GeoAddress address = addresses.get(0);
// Get the coordinates for the address
GeoCoordinate coords = address.getCoordinate();
// Longitude
double longitude = coords.getLongitude();
// Latitude
double latitude = coords.getLatitude();
3 голосов
/ 04 июня 2011

Вот как я это сделал в итоге

public static String getCordinates(String address,String county) throws IOException, ParserConfigurationException, SAXException{
    String thisLine;

    address = address.replace(",", "+");
    address = address.replace(" ", "+");
    county = county.replace(" ", "");

    String fullAddress = address+"+"+county;
    System.out.println(fullAddress);

    URL url = new URL("http://maps.google.com/maps/geo?q="+fullAddress+"&output=xml&key=ABQIAAAANGTAqDyDam_07aWkklK2NBSD41w" +
            "X8VhCBpuiDVjGbFNuXE31lhQB8Gkwy-wmYbmaHIbJtfnlR9I_9A");

    BufferedReader theHTML = new BufferedReader(new InputStreamReader(url.openStream()));

    FileWriter fstream = new FileWriter("url.xml");
    BufferedWriter out = new BufferedWriter(fstream);
    while ((thisLine = theHTML.readLine()) != null)
        out.write(thisLine);
    out.close();

    File file = new File("url.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    NodeList nl = doc.getElementsByTagName("code");
    Element n = (Element)nl.item(0);
    String st = n.getFirstChild().getNodeValue();

    if (st.equals("200"))
    {
        NodeList n2 = doc.getElementsByTagName("coordinates");
        Element nn = (Element)n2.item(0);
        String st1 = nn.getFirstChild().getNodeValue();


        return st1;
    }
    else
    {
        return null;
    }


}
2 голосов
/ 04 июня 2011

Вы можете использовать это Google API

0 голосов
/ 15 августа 2016

Использование API с restAssurd - это простой способ:

 public static String[] getCoordinates(String address, String county) throws IOException, ParserConfigurationException, SAXException {

    address = address.replace(",", "+");
    address = address.replace(" ", "+");
    county = county.replace(" ", "");

    String fullAddress = address+"+"+county;
    System.out.println(fullAddress);

    URL url = new URL("http://maps.google.com/maps/api/geocode/json?address="+fullAddress+"&sensor=false");

    Response res = given().
            when().
            get(url);
    JsonPath jp = new JsonPath(res.asString());
    String lat = jp.get("results.geometry.location.lat").toString();
    String lng = jp.get("results.geometry.location.lng").toString();

    String[] location = new String[2];
    location[0] = lat;
    location[1] = lng;

    return location;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...