Hai everyone,

I need to find the Latitude and Longitude values for any one of the Particular location from the Google Maps, using pure java class.

I know, it's done by using javascript, but i need to use pure java class not javascript.

If we provide full address of the location or city or state or country or pincode, then we get the Latitude and Longitude values for the particulars in the google maps.

If anyone having any sample code, it's very useful for me, please help me.

can anyone know this, please healp me

waiting for replies.

Dani AI

Generated

Short answer: you can do this entirely in Java by calling a server-side geocoding HTTP API (for example Google Geocoding) and parsing the JSON response. That avoids any browser JavaScript. is correct that Java and JavaScript are different runtimes, but you do not need client-side code to obtain latitude/longitude.

Steps (high level):

  • Get an API key and enable a geocoding service (see Google Geocoding docs: Geocoding API Overview).
  • Build an HTTPS GET with the URL-encoded address and your key.
  • Call that URL from Java, parse the JSON, take the first result’s geometry.location.lat/lng.
  • Check the API status field and handle ZERO_RESULTS, OVER_QUERY_LIMIT, etc.

Example (Java 11 HttpClient + Gson):

String q = URLEncoder.encode("1600 Amphitheatre Parkway, Mountain View, CA", StandardCharsets.UTF_8);
String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + q + "&key=" + API_KEY;
HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder(URI.create(url)).GET().build();
HttpResponse<String> res = client.send(req, BodyHandlers.ofString());

JsonObject root = JsonParser.parseString(res.body()).getAsJsonObject();
if ("OK".equals(root.get("status").getAsString())) {
  JsonObject loc = root.getAsJsonArray("results")
                       .get(0).getAsJsonObject()
                       .getAsJsonObject("geometry")
                       .getAsJsonObject("location");
  double lat = loc.get("lat").getAsDouble();
  double lng = loc.get("lng").getAsDouble();
}

Notes and cautions:

  • Cache results to avoid hitting quotas; check API limits and billing on Google’s docs.
  • If you already have coordinates (moving vehicle scenario raised by ), you do not need geocoding repeatedly — send lat/lng updates from the server to the client (WebSocket/polling) and update the map marker.
  • Free alternative: OSM/Nominatim (see Nominatim API Overview), but respect usage policies and rate limits.

Recommended Answers

All 3 Replies

google search on topic here

Hi ,

I need to find the Latitude and Longitude values for moving vehicle from the Google Maps, using javascripts.

Can you give the sample code, please help me.

Than you are in wrong forum section, JAVA != JavaScript ! ! !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.