如何以编程方式(合法地)获取街道地址的经度和纬度

问题描述 投票:73回答:15

据说,可以从Google地图或某些此类服务获取此信息。 (美国地址不够好。)

web-services api geolocation geocoding street-address
15个回答
70
投票

您正在寻找的术语是地理编码,是的Google确实提供此服务。


2
投票

R代码获取街道地址的纬度和经度

# CODE TO GET THE LATITUDE AND LONGITUDE OF A STREET ADDRESS WITH GOOGLE API
addr <- '6th Main Rd, New Thippasandra, Bengaluru, Karnataka'  # set your address here
url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='')  # construct the URL
doc = xmlTreeParse(url) 
root = xmlRoot(doc) 
lat = xmlValue(root[['result']][['geometry']][['location']][['lat']]) 
long = xmlValue(root[['result']][['geometry']][['location']][['lng']]) 
lat
[1] "12.9725020"
long
[1] "77.6510688"

2
投票

如果你想在Python中这样做:

import json, urllib, urllib2

address = "Your address, New York, NY"

encodedAddress = urllib.quote_plus(address)
data = urllib2.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + '&sensor=false').read()
location = json.loads(data)['results'][0]['geometry']['location']
lat = location['lat']
lng = location['lng']
print lat, lng

请注意,如果Google看到超过一定数量的请求,它似乎会限制请求,因此您确实希望在HTTP请求中使用API​​密钥。


1
投票

我有一批100,000条记录要进行地理编码并遇到了Google API的限制(因为它是针对内部企业应用程序的,我们不得不升级到他们的高级服务,这是10美元以上)

所以,我使用了这个:http://geoservices.tamu.edu/Services/Geocode/BatchProcess/ - 他们也有一个API。 (总费用约为200美元)


1
投票

您可以在JavaScript中尝试使用kohat这样的城市

var geocoder = new google.maps.Geocoder();
var address = "kohat";
geocoder.geocode( { 'address': address}, function(results, status) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude+" and "+longitude);
  } 
});

enter image description here


1
投票

在python中使用qazxsw poi用于获取纬度,经度,邮政编码等。这是工作示例代码..

 geopy PyPI 

在Nominatim - 有些地址无法正常工作,所以我只是尝试了from geopy.geocoders import Nominatim geolocator = Nominatim(user_agent="your-app-id") location = geolocator.geocode("Your required address ") if location: print('\n Nominatim ADDRESS :',location.address) print('\n Nominatim LATLANG :',(location.latitude, location.longitude)) print('\n Nominatim FULL RESPONSE :',location.raw) else: print('Cannot Find') 。 它正确返回。 Mapquest每月提供15000个免费计划交易。这对我来说已经足够了。

示例代码:

MapQuest

希望能帮助到你。


0
投票

我知道这是一个古老的问题,但谷歌改变方式来获得经常性的经纬度。

HTML代码

import geocoder
g = geocoder.mapquest("Your required address ",key='your-api-key')

for result in g:
   # print(result.address, result.latlng)
   print('\n mapquest ADDRESS :',result.address,result.city,result.state,result.country)
   print('\n mapquest LATLANG :', result.latlng)
   print('\n mapquest FULL RESPONSE :',result.raw)

JavaScript代码

<form>
    <input type="text" name="address" id="address" style="width:100%;">
    <input type="button" onclick="return getLatLong()" value="Get Lat Long" />
</form>
<div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
</div>

有关更多信息,请参阅此链接<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script> <script> function getLatLong() { var address = document.getElementById("address").value; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var latitude = results[0].geometry.location.lat(); document.getElementById("latbox").value=latitude; var longitude = results[0].geometry.location.lng(); document.getElementById("lngbox").value=longitude; } }); } </script>


22
投票

除了前面提到的Google geocoding web service,还有一个竞争的service provided by Yahoo。在最近通过用户交互完成地理编码的项目中,我包括对两者的支持。原因是我发现,特别是在美国之外,他们对更加模糊的地点的处理差异很大。有时谷歌会得到最好的答案,有时候是雅虎。

需要注意的是:如果Google真的认为他们不知道你的位置在哪里,他们会返回602错误,表示失败。另一方面,雅虎如果可以从你的不良地址剥离一个城市/省/州/等,将返回该镇中心的位置。因此,您必须注意结果,看看它们是否真的是您想要的。某些结果中有辅助字段可以告诉您:雅虎将此字段称为“精确”,Google将其称为“准确性”。


15
投票

如果你想在不依赖服务的情况下这样做,那么你从美国人口普查下载TIGER Shapefiles

你抬头看看你感兴趣的街道,它将有几个部分。每个段都有一个起始地址和结束地址,并沿着该段进行插值,以找到您的门牌号所在段的位置。

这将为您提供lon / lat对。

但请记住,在线服务会使用大量的地址检查和更正,您必须复制这些内容以获得良好的结果。

另请注意,与免费数据一样好,它并不完美 - 最新的街道不在那里(它们可能在Google使用的数据中),并且由于调查不准确,街道可能会偏离其实际位置一定数量。但是对于98%的地理编码需求,它可以完美地运行,是免费的,并且您可以控制所有内容,因此您可以减少应用中的依赖关系。

Openstreetmaps的目标是绘制世界上的所有内容,尽管它们并不存在,因为它们在CC许可下提供数据值得关注

但是,许多(大多数?)其他国家/地区仅通过您需要支付费用的政府或服务进行映射。如果您不需要对非常多的数据进行地理编码,那么使用Google,Yahoo或其他一些免费的全球地图服务可能就足够了。

如果您必须对大量数据进行地理编码,那么最好通过租用来自主要提供商(如teleatlas)的地图数据来提供服务。

-亚当


9
投票

您还可以尝试使用OpenStreetMap NameFinder,其中包含(可能)整个世界的开源,类似wiki的街道数据。 NameFinder将在8月底停止存在,但Nominatim是它的替代品。


7
投票

Google要求您显示包含其数据的Google地图,每天最多2.5k(25k)HTTP请求。有用,但你必须看用法。

他们确实有

http://groups.google.com/group/Google-Maps-API/web/resources-non-google-geocoders

(谷歌已经删除了这个。如果你看到重复或缓存,我会链接到那个。)

...我发现GeoNames有可下载的数据库,免费的网络服务和商业网络服务。


4
投票

如果您的网站免费供消费者使用,Google的服务条款将允许您免费使用其地理编码API。如果没有,您将必须获得企业地图的许可。


3
投票

用于Drupal和PHP(并且易于修改):

function get_lat_long($address) {
  $res = drupal_http_request('http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false');
  return json_decode($res->data)->results[0]->geometry->location;
}

2
投票

您可以在此处查看Google Maps API文档,了解相关信息:

http://code.google.com/apis/maps/documentation/services.html#Geocoding

对于使用Live Maps的国际地址,您似乎也可以这样做:

http://virtualearth.spaces.live.com/blog/cns!2BBC66E99FDCDB98!1588.entry


2
投票

您也可以使用Microsoft的MapPoint Web服务执行此操作。

这是一篇博客文章,解释了如何:http://www.codestrider.com/BlogRead.aspx?b=b5e8e275-cd18-4c24-b321-0da26e01bec5

© www.soinside.com 2019 - 2024. All rights reserved.