java 当我在java中有纬度和经度时如何获取位置的地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12797198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get location's address when i have latitude and longitude in java?
提问by ravi
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?
我从 GPS 设备获得一定的纬度和经度。我必须知道那个地方的地址。我怎么能在java中做到这一点。当我知道纬度和经度时,谷歌在java中提供了任何API来知道地址吗?
回答by Bhola
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
Web 服务客户端用于从提供某些 Web 服务的另一台服务器访问资源
Using this service, you can get address use of Latitude, Longitude
使用此服务,您可以获得纬度、经度的地址使用
Google are Providing Free web service client URI That is
谷歌正在提供免费的网络服务客户端 URI 即
"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
使用该服务您可以从 Google place API 获取 JSON 格式的所有类型的地址
After You can Find Your required objects and value from JSON data
在您可以从 JSON 数据中找到您需要的对象和值之后
I give You formatted_address from that JSON data
我从那个 JSON 数据中给你 formatted_address
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;
}
}
回答by dngfng
Google's Geocodingand Places APIdoes exactly what you want.