How to get Latitude and Longitude for particular city or street without using google map?
Asked Answered
A

4

6

How to get Latitude and Longitude for particular city or street or location without using google map? For example user entered city name is "Chennai" and i need to show only Latitude and Longitude for that city. How to do this?

Agnes answered 11/3, 2011 at 7:13 Comment(0)
C
10

This is called Geocoding, and requires matching a place name up to a latitude and longitude.

You can either code a known list of place name/lat-lons into your app, read them in at runtime and search through them when required, or you can use an online Geocoding service such as this one from Yahoo, or this one from Google.

Cerebellum answered 11/3, 2011 at 9:44 Comment(0)
E
2

check Obtaining geographic coordinates at Wikipedia page

Ejecta answered 11/3, 2011 at 7:21 Comment(0)
A
2

Google provides reverse geocoding - please go through this link

also for example below web service gives you the address of lat long provided in the parameter but hte response is in json, see this link

if you want to get response in xml then check here

Arrowroot answered 4/5, 2011 at 10:46 Comment(0)
C
1

I had the same problem.. Finally I fixed it by getting the lat long from the server side. This link has the sample code for getting lat and long from java. Hope it helps some one.

private static final String GEOCODE_REQUEST_URL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&";
    private static HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    String strLatitude;
    String strLongtitude;
        public void getLongitudeLatitude(String address) {
            try {
                StringBuilder urlBuilder = new StringBuilder(GEOCODE_REQUEST_URL);
                if (StringUtils.isNotBlank(address)) {
                    urlBuilder.append("&address=").append(URLEncoder.encode(address, "UTF-8"));
                }

                final GetMethod getMethod = new GetMethod(urlBuilder.toString());
                try {
                    httpClient.executeMethod(getMethod);
                    Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet());

                    int data = reader.read();
                    char[] buffer = new char[1024];
                    Writer writer = new StringWriter();
                    while ((data = reader.read(buffer)) != -1) {
                            writer.write(buffer, 0, data);
                    }

                    String result = writer.toString();
                    System.out.println(result.toString());

                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader("<"+writer.toString().trim()));
                    Document doc = db.parse(is);

                    strLatitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lat/text()");
                    System.out.println("Latitude:" + strLatitude);

                    strLongtitude = getXpathValue(doc,"//GeocodeResponse/result/geometry/location/lng/text()");
                    System.out.println("Longitude:" + strLongtitude);


                } finally {
                    getMethod.releaseConnection();
                }
            } catch (Exception e) {
                 e.printStackTrace();
            }
        }

        private String getXpathValue(Document doc, String strXpath) throws XPathExpressionException {
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xPath.compile(strXpath);
            String resultData = null;
            Object result4 = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result4;
            for (int i = 0; i < nodes.getLength(); i++) {
                resultData = nodes.item(i).getNodeValue();
            }
            return resultData;


    }
Cherey answered 4/5, 2012 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.