Reputation: 75
i am getting certain latitude and longitude from the GPS device. I have to know the address of the place. How that i can do in java. Is there any API that google provides in java to know the address when i know the latitude and longitude?
Upvotes: 6
Views: 14028
Reputation: 1676
If you're using Java and want to have offline reverse geocoding you can try with the Countryboundaries library.
<!-- https://mvnrepository.com/artifact/de.westnordost/countryboundaries -->
<dependency>
<groupId>de.westnordost</groupId>
<artifactId>countryboundaries</artifactId>
<version>CHECK VERSIONS</version>
</dependency>
This WILL NOT give you an up-to-date detailed address but you'll get at least the country of a location. It will be quicker then online API and free of charge.
Upvotes: 0
Reputation: 412
In the following Example, there are included two main concepts.
Web Services client is used to access resource from another server which is provide some web service
Using this service, you can get address use of Latitude, Longitude
Google are Providing Free web service client URI That is
"https://maps.googleapis.com/maps/api/geocode/json?latlng=<LATITUDE>,<LONGITUDE> &key=<ENTER YOUR PLACE API KEY HEAR>
Use that service You can get all type of address in JSON format from Google place API
After You can Find Your required objects and value from JSON data
I give You formatted_address from that JSON data
There are Following Code to Get Location Address Using Latitude,longitude
package clientpkg;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class MapAdd
{
public static void main(String...aa)
{
try
{
System.out.println("Finding Address...\n");
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
String jsonaddresss = target.request().accept(MediaType.APPLICATION_JSON).get(String.class);
String address = adres(jsonaddresss);
System.out.println("Perfect Address is : >>"+address);
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
private static URI getBaseURI()
{
return UriBuilder.fromUri("https://maps.googleapis.com/maps/api/geocode/json?latlng=32.263200,50.778187&key=<ENTER YOUR PLACE API KEY HEAR >").build();
}
private static String adres(String jsonaddresss)
{
try
{
JSONParser parser = new JSONParser();
Object obj = parser.parse(jsonaddresss);
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("results");
//System.out.println("Message"+msg.get(1)); //Get Second Line Of Result
JSONObject jobj = (JSONObject) parser.parse(msg.get(1).toString()); // Parsse it
String perfect_address = jobj.get("formatted_address").toString();
jsonaddresss = perfect_address;
}
catch(Exception e)
{
jsonaddresss = "Error In Address"+e;
}
return jsonaddresss;
}
}
Upvotes: 5