接受地址并返回经度和纬度坐标的Java函数 [英] Java function that accepts address and returns longitude and latitude coordinates

查看:82
本文介绍了接受地址并返回经度和纬度坐标的Java函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的java函数,该函数可以接受一个地址并返回该地址的经度和纬度坐标.根据我的研究,这是我到目前为止发现的:

I am looking for a simple java function that can accept an address and return the longitude and latitude coordinates for that address. Based on my research this is what i found thus far:

我有点不确定如何实现这一点.我不确定要调用不同的方法需要什么顺序.

I am abit unsure how to implements this. I am not sure what order i will need to call the different methods.

例如我要这样做吗? :

E.g. would i have to do this? :

String address = 'Some Place, Venezuela';
Geocoder geo = new Geocoder();

String url = geo.encode(address);

String encodeUrl = geo.urlEncode(url);

GLatLng latLng = geo.decode(encodeUrl);

如果您曾经使用过其他工具,可以随时分享.

If there are others that you have used feel free to share it.

/*
*
* ==============================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.wicketstuff.gmap.geocoder;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.StringTokenizer;
import org.wicketstuff.gmap.api.GLatLng;

/**
* Geocoder. See: http://www.google.com/apis/maps/documentation/services.html# Geocoding_Direct
*
* @author Thijs Vonk
*/
public class Geocoder implements Serializable
{

    private static final long serialVersionUID = 1L;
    // Constants
    public static final String OUTPUT_CSV = "csv";
    public static final String OUTPUT_XML = "xml";
    public static final String OUTPUT_KML = "kml";
    public static final String OUTPUT_JSON = "json";
    private final String output = OUTPUT_CSV;

    public Geocoder()
    {
    }

    public GLatLng decode(String response) throws GeocoderException
    {

        StringTokenizer gLatLng = new StringTokenizer(response, ",");

        String status = gLatLng.nextToken();
        gLatLng.nextToken(); // skip precision
        String latitude = gLatLng.nextToken();
        String longitude = gLatLng.nextToken();

        if (Integer.parseInt(status) != GeocoderException.G_GEO_SUCCESS)
        {
            throw new GeocoderException(Integer.parseInt(status));
        }

        return new GLatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
    }

    /**
* builds the google geo-coding url
*
* @param address
* @return
*/
    public String encode(final String address)
    {
        return "http://maps.google.com/maps/geo?q=" + urlEncode(address) + "&output=" + output;
    }

    /**
* @param address
* @return
* @throws IOException
*/
    public GLatLng geocode(final String address) throws IOException
    {
        InputStream is = invokeService(encode(address));
        if (is != null)
        {
            try
            {
                String content = org.apache.wicket.util.io.IOUtils.toString(is);
                return decode(content);
            }
            finally
            {
                is.close();
            }
        }
        return null;
    }

    /**
* fetches the url content
*
* @param address
* @return
* @throws IOException
*/
    protected InputStream invokeService(final String address) throws IOException
    {
        URL url = new URL(address);
        return url.openStream();
    }

    /**
* url-encode a value
*
* @param value
* @return
*/
    private String urlEncode(final String value)
    {
        try
        {
            return URLEncoder.encode(value, "UTF-8");
        }
        catch (UnsupportedEncodingException ex)
        {
            throw new RuntimeException(ex.getMessage());
        }
    }
}

推荐答案

据我所知,如果您使用的是Google Maps API,则需要在应用程序中显示地图以使用数据.

If you're using Google Maps API, you need to show a map with your application in order to use the data, as far as I understand their TOS.

这是GitHub上的示例,该示例使用地址验证SmartyStreets的API可以验证美国地址并获取坐标.这种方式对许可证的限制要宽得多. (我在SmartyStreets工作,并实际编写了这个示例.)

Here's an example on GitHub that uses an address verification API from SmartyStreets to verify a US address and get the coordinates. There license restrictions are much more lenient this way. (I work at SmartyStreets and actually wrote this particular example.)

或者,有一个用于 exoanalytic在Java中进行批量地理编码的小型库.

这篇关于接受地址并返回经度和纬度坐标的Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆