简体   繁体   English

如何使用 HTML5 地理定位或 Google API 获取地址经纬度?

[英]How do I get an addresses latitude-longitude using HTML5 geolocation or Google API?

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.如果我可以使用 HTML5 地理定位或任何 Google API 获得纬度和经度,这将有所帮助。 What is the solution?解决办法是什么?

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.如果浏览器支持地理定位并且getCurrentPosition运行成功,则会调用成功函数。 And then in the function successFunction然后在函数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 的更多信息

Flyleaf, I work at SmartyStreets where we also provide an API to get the coordinates of addresses; Flyleaf,我在SmartyStreets工作,我们也提供 API 来获取地址坐标; it's called LiveAddress API .它被称为LiveAddress 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.正如 Bart 所提到的,Google 的 TOS 不允许您在不显示 Google 地图的情况下进行地理编码,而且您无法存储结果。 If you're looking to locate an address as you've suggested, I recommend something like LiveAddress which will actually verify the validity of the address as well -- these other APIs and HTML5 will not do that.如果您想按照您的建议查找地址,我建议您使用 LiveAddress 之类的方法,它实际上也可以验证地址的有效性——这些其他 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);
});

I found the best option to be to use OpenStreetMap directly via a sub-project ( Nominatim ) API they have.我发现最好的选择是通过他们拥有的子项目 ( Nominatim ) API 直接使用OpenStreetMap

API documentation API 文档

Example REST call: http://nominatim.openstreetmap.org/reverse?format=xml&lat=35.958&lon=-83.952&zoom=18REST调用示例: 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.请注意,虽然他们的服务条款确实允许从浏览器 JavaScript 直接在客户端使用,但他们不希望大量使用并建议使用您的服务器作为代理来添加缓存,因此可以轻松切换。 The data is available for download, and instructions are provided for setting up a locally query-able database of the data.数据可供下载,并提供了设置本地可查询数据数据库的说明。

Check out Gisgraphy a 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. (g) 在没有谷歌地图的情况下不得使用内容。 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.您不得在没有相应 Google 地图的情况下使用或显示内容,除非 Maps API 文档中明确允许您这样做,或获得 Google 的书面许可。 In any event, you must not use or display the Content on or in conjunction with a non-Google map.在任何情况下,您都不得在非 Google 地图上或与非 Google 地图一起使用或显示内容。 For example, you must not use geocodes obtained through the Service in conjunction with a non-Google map.例如,您不得将通过服务获得的地理编码与非 Google 地图结合使用。 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.再举一个例子,您不得在非 Google 地图旁边显示街景图像,但您可以在没有相应 Google 地图的情况下显示街景图像,因为 Maps API 文档明确允许您这样做。

So with the Google API you must use a Google Map .因此,对于 Google API,您必须使用 Google Map So if you need a map you're OK.所以如果你需要一张地图,你就可以了。

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 thegetCurrentPosition method is for .这就是getCurrentPosition方法的用途

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在功能之外使用HTML5访问经纬度地理位置 - How to access Latitude and longitude geolocation using HTML5 outside of function 如何在Google Maps Geolocation API中获取笔记本电脑(wifi)的纬度和经度? - How get latitude and longitude of laptop (wifi) in Google Maps Geolocation API? 使用 Google API 获取多个地址的经纬度 - Get the latitude and longitude of multiple addresses using Google API HTML5地理定位中的经度和纬度半径 - Radius of longitude and latitude in HTML5 geolocation Cordova-我怎么知道我的应用程序是使用HTML5 Geolocation API还是Cordova Geolocation插件? - Cordova - how do I know if my app is using the HTML5 Geolocation API or the cordova geolocation plugin? 将经纬度参数发送到Google地图 - Send latitude-longitude argument to Google Maps 如何设置纬度和经度以使用 Google Chrome 调试 Geolocation API? - How can I set my latitude and longitude for debugging the Geolocation API with Google Chrome? 如何使用google map api在php中获取当前的纬度和经度? - How to get current latitude and longitude in php using google map api? 如何使用Google API和表格获取任何城市的纬度和经度? - How to get Latitude and Longitude of any City using Google API and a form? 纬度-经度地址出现OVER_QUERY_LIMIT错误 - OVER_QUERY_LIMIT error with latitude-longitude addresses
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM