molleman molleman - 1 year ago 64
Java Question

Retreive Coordinates from google maps api

I Have a list of addresses, i now want to retrieve their longitudes and latitutdes. i would like to use the google maps api through java. How would i go about retrieving just one set of co-ords for one address. (cause i could then easily implement for multiple)

Answer Source

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;

    URL url = new URL(""+fullAddress+"&output=xml&key=ABQIAAAANGTAqDyDam_07aWkklK2NBSD41w" +

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

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

    File file = new File("url.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    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;
        return null;