欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

如何在网站上添加谷歌定位信息_html5教程技巧

程序员文章站 2022-04-07 08:10:00
...
我们可以很容易地使用HTML5导航对象获取当前位置。请按照以下步骤来获得城市/国家的细节。

首先包括jQuery库

JavaScript Code复制内容到剪贴板
  1. src="jquery.js">
  2. jQuery(function() {
  3. //call navigator object to get latitude and logtitute
  4. if(navigator.geolocation) {
  5. //getting current latitude and longtitude
  6. navigator.geolocation.getCurrentPosition(function(position) {
  7. //get location details based on latitude and longtitude from Google
  8. var jsonUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+position.coords.latitude+","+position.coords.longitude
  9. jQuery.getJSON(jsonUrl , function( data ) {
  10. var results = data.results[0];
  11. console.info(results); //check firebug
  12. alert(results.address_components[5].long_name); //City
  13. alert(results.address_components[6].long_name); //State
  14. alert(results.address_components[7].long_name); //Country
  15. },
  16. function(){
  17. alert('Failed to get Location Details...');
  18. });
  19. return false;
  20. });
  21. }});

以上这篇如何在网站上添加谷歌定位信息就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

原文地址:http://www.cnblogs.com/mivi/archive/2016/04/16/5398183.html

相关标签: 网站 谷歌定位