gpt4 book ai didi

接受地址并返回经度和纬度坐标的Java函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:29 25 4
gpt4 key购买 nike

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

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

例如我必须这样做吗? :

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

String url = geo.encode(address);

String encodeUrl = geo.urlEncode(url);

GLatLng latLng = geo.decode(encodeUrl);

如果您还使用过其他产品,请随时分享。

/*
*
* ==============================================================================
* 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,据我了解其 TOS,您需要在应用程序中显示 map 才能使用数据。

Here's an example on GitHub它使用 SmartyStreets 的地址验证 API 来验证美国地址并获取坐标。这种方式的许可限制要宽松得多。 (我在 SmartyStreets 工作,实际上编写了这个特定的示例。)

或者,还有一个小型库 massively-batch geocoding in Java by exoanalytic .

关于接受地址并返回经度和纬度坐标的Java函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com