简体   繁体   English

从谷歌地图 api 检索坐标

[英]Retrieve Coordinates from google maps api

I have a list of addresses, i now want to retrieve their longitudes and latitudes.我有一个地址列表,我现在想检索它们的经度和纬度。 i would like to use the google maps api through java.我想使用谷歌地图 api 到 java。 How would i go about retrieving just one set of co-ords for one address.我将如何 go 仅检索一个地址的一组坐标。 (cause i could then easily implement for multiple) (因为我可以轻松地实现多个)

You may also use the Google Geocoder Java API GeoGoogle like this:您也可以像这样使用 Google 地理编码器 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();

This is how i did it in the end这就是我最后的方式

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;
    }


}

You can use this Google API你可以用这个谷歌 API

Using API with restAssurd is an easy way:将 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM