How to get Latitude and Longitude for a specific city or street without using a Google map?

How to get Latitude and Longitude for a specific city or street or location without using a Google map? For example, the name of the city entered by the user is β€œChennai”, and I need to show only the latitude and longitude for this city. How to do it?

+6
java latitude-longitude java-me jsr179
source share
4 answers

This is called geocoding and requires the location name to match latitude and longitude.

You can either encode a known list of place names / lat -lons in your application, read them at runtime and search them when needed, or use an online geocoding service such as this one from Yahoo or this one from Google .

+10
source share

check Get geographic coordinates on Wikipedia page

+2
source share

Google provides reverse geocoding - follow this link

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

if you want to get the answer in xml and then here

+2
source share

I had the same problem. Finally, I fixed it by getting lat from the server. This link contains sample code for getting lat and long from java. Hope this helps someone.

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; } 
+1
source share

All Articles