Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中从地图中获取纬度和经度_Java_Google Maps_Latitude Longitude - Fatal编程技术网

如何在java中从地图中获取纬度和经度

如何在java中从地图中获取纬度和经度,java,google-maps,latitude-longitude,Java,Google Maps,Latitude Longitude,我有一个java项目,在这个项目中,我需要从地图中手动指定一个地方的纬度和经度。 实际上,我使用以下代码通过地址获取纬度和经度: public class LongLatService { private static final String GEOCODE_REQUEST_URL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&"; private static HttpClient h

我有一个java项目,在这个项目中,我需要从地图中手动指定一个地方的纬度和经度。 实际上,我使用以下代码通过地址获取纬度和经度:

public class LongLatService {

    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());

    public static void main(String[] args) throws Exception {
        LongLatService tDirectionService = new LongLatService();
        tDirectionService.getLongitudeLatitude("Rue Delangle, 58210 Varzy, France");
    }

    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);

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

                String 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;
    }

}
公共类远程服务{
私有静态最终字符串GEOCODE\u REQUEST\u URL=”http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&";
私有静态HttpClient HttpClient=新HttpClient(新的多线程HttpConnectionManager());
公共静态void main(字符串[]args)引发异常{
LongLatService tDirectionService=新的LongLatService();
tDirectionService.GetLongitudRelational(“法国瓦兹市德兰格街58210号”);
}
public void GetLongitudRelativity(字符串地址){
试一试{
StringBuilder urlBuilder=新的StringBuilder(地理编码\u请求\u URL);
if(StringUtils.isNotBlank(地址)){
urlBuilder.append(“&address=”).append(urlcoder.encode(地址,“UTF-8”);
}
final-GetMethod=new-GetMethod(urlBuilder.toString());
试一试{
httpClient.executeMethod(getMethod);
Reader Reader=新的InputStreamReader(getMethod.getResponseBodyAsStream(),getMethod.getResponseCharSet());
int data=reader.read();
char[]buffer=新字符[1024];
Writer-Writer=新的StringWriter();
而((数据=读卡器。读取(缓冲区))!=-1){
writer.write(缓冲区,0,数据);
}
字符串结果=writer.toString();
System.out.println(result.toString());
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
InputSource is=新的InputSource();

is.setCharacterStream(新StringReader("为此,我建议您使用。这是一个由Google为全球Java开发人员开发的库,用于使用各种Google地图功能,如地理编码、方向API、距离矩阵API等。在您的情况下,您可能正在使用返回指定地址的LatLng的地理编码API。您只需集成在您的项目中使用这个库(可以与Maven和Gradle一起使用),然后只需几行代码就可以为您提供通过Google地图传递的地址的纬度和经度

以下是地理代码API需要使用的代码:

GeoApiContext context = new GeoApiContext().setApiKey("AIza...");
GeocodingResult[] results =  GeocodingApi.geocode(context,
    "1600 Amphitheatre Parkway Mountain View, CA 94043").await();
System.out.println(results[0].formattedAddress);
如果不想使用此库编写自己的代码,可以参考中的代码示例


希望这有帮助!!

你说的“从地图”是什么意思?具体的经度和纬度是什么?我的意思是我想要一个包含地图的框架或网页,然后我从地图中选择一个特定的地方,我想从我的java项目中获得经度和纬度