Javascript 如何使用 HTML5 地理定位或 Google API 获取地址经纬度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11667838/
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 do I get an addresses latitude-longitude using HTML5 geolocation or Google API?
提问by flyleaf
I am getting baffled with a problem that is, I can get the location of a user from Facebook and all I need is to get the latitude and longitude of that location, but I still couldn't find a solution.
我对一个问题感到困惑,我可以从 Facebook 获取用户的位置,我需要的只是获取该位置的纬度和经度,但我仍然找不到解决方案。
It will help if I can get that latitude and longitude using an HTML5 geolocation or any Google API. What is the solution?
如果我可以使用 HTML5 地理定位或任何 Google API 获得纬度和经度,这将有所帮助。解决办法是什么?
回答by shwetank
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successFunction, errorFunction); } else { alert('It seems like Geolocation, which is required for this page, is not enabled in your browser. Please use a browser which supports it.'); }
If the browser supports geolocation and if getCurrentPosition
runs successfully, then a success function is called. And then in the function successFunction
如果浏览器支持地理定位并且getCurrentPosition
运行成功,则调用成功函数。然后在函数中successFunction
have
有
function successFunction(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
console.log('Your latitude is :'+lat+' and longitude is '+long);
}
Read more on the Geolocation API here
在此处阅读有关 Geolocation API 的更多信息
- http://dev.opera.com/articles/view/how-to-use-the-w3c-geolocation-api/(Disclaimer: my article)
- http://dev.w3.org/geo/api/spec-source.html(The Spec)
回答by Split Your Infinity
Check out Gisgraphya free an open source geo service.
查看Gisgraphy一个免费的开源地理服务。
Everyone will tell you to use the Google Maps API but read the terms of use.
每个人都会告诉您使用 Google Maps API,但请阅读使用条款。
(g) No Use of Content without a Google Map. You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google. In any event, you must not use or display the Content on or in conjunction with a non-Google map. For example, you must not use geocodes obtained through the Service in conjunction with a non-Google map. As another example, you must not display Street View imagery alongside a non-Google map, but you may display Street View imagery without a corresponding Google map because the Maps APIs Documentation explicitly permits you to do so.
(g) 在没有谷歌地图的情况下不得使用内容。您不得在没有相应 Google 地图的情况下使用或显示内容,除非 Maps API 文档中明确允许您这样做,或获得 Google 的书面许可。在任何情况下,您都不得在非 Google 地图上或与非 Google 地图一起使用或显示内容。例如,您不得将通过服务获得的地理编码与非 Google 地图结合使用。再举一个例子,您不得在非 Google 地图旁边显示街景图像,但您可以在没有相应 Google 地图的情况下显示街景图像,因为 Maps API 文档明确允许您这样做。
So with the Google API you must use a Google Map. So if you need a map you're OK.
因此,对于 Google API,您必须使用 Google Map。所以如果你需要一张地图,你就可以了。
回答by Matt
Flyleaf, I work at SmartyStreetswhere we also provide an API to get the coordinates of addresses; it's called US Street Address API.
Flyleaf,我在SmartyStreets工作,我们也提供 API 来获取地址坐标;它被称为美国街道地址 API。
As Bart mentioned, Google's TOS won't allow you to geocode without showing a Google Map, and what's more is you can't store the results. If you're looking to locate an addressas you've suggested, I recommend something like US Street Address API which will actually verify the validity of the address as well -- these other APIs and HTML5 will not do that.
正如 Bart 所提到的,Google 的 TOS 不允许您在不显示 Google 地图的情况下进行地理编码,而且您无法存储结果。如果您想按照您的建议查找地址,我建议您使用 US Street Address API 之类的东西,它实际上也会验证地址的有效性——这些其他 API 和 HTML5 不会这样做。
Hope you find something that works for you. The question right now is a little vague, but maybe this will help. It might go something like this (with Javascript):
希望你找到适合你的东西。现在的问题有点模糊,但也许这会有所帮助。它可能是这样的(使用 Javascript):
LiveAddress.geocode(address, function(geo) {
alert("The address is at: " + geo.coords);
});
Update: There is also an international address validation APIversion as well.
更新:还有一个国际地址验证 API版本。
回答by DavidJ
I found the best option to be to use OpenStreetMapdirectly via a sub-project (Nominatim) API they have.
我发现最好的选择是通过他们拥有的子项目 ( Nominatim) API直接使用OpenStreetMap。
Example RESTcall: http://nominatim.openstreetmap.org/reverse?format=xml&lat=35.958&lon=-83.952&zoom=18
REST调用示例:http: //nominatim.openstreetmap.org/reverse?format=xml&lat=35.958&lon=-83.952&zoom=18
Note that while their terms of service does allow direct client-side use from browser JavaScript, they don't want high-volume use and suggest using your server as a proxy to add caching and so it can be easily switched. The data is available for download, and instructions are provided for setting up a locally query-able database of the data.
请注意,虽然他们的服务条款确实允许从浏览器 JavaScript 直接在客户端使用,但他们不希望大量使用并建议使用您的服务器作为代理来添加缓存,因此可以轻松切换。数据可供下载,并提供了设置本地可查询数据数据库的说明。
回答by CBroe
It will help if I can get that latitude and longitude using an HTML5 geolocation
如果我可以使用 HTML5 地理定位获得纬度和经度,这将有所帮助
Well, then just do exactly that – where exactly is your problem with it?
好吧,那么就这样做吧——你的问题究竟出在哪里?
That's what the getCurrentPosition
methodis for.