通过html5和javascript获取用户地理位置

前端之家收集整理的这篇文章主要介绍了通过html5和javascript获取用户地理位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过 HTML5 geolcation api获取用户的地理位置,我使用以下代码段:
  1. if (navigator.geolocation) {
  2. var timeoutVal = 10 * 1000 * 1000;
  3. navigator.geolocation.getCurrentPosition(
  4. displayPosition,displayError,{ enableHighAccuracy: true,timeout: timeoutVal,maximumAge: 0 }
  5. );
  6. }
  7. else {
  8. // DO SOME STUFF HERE
  9. }
  10.  
  11.  
  12. function displayPosition(position) {
  13.  
  14. // configuration
  15. var myZoom = 12;
  16. var myMarkerIsDraggable = true;
  17. var myCoordsLenght = 6;
  18. var defaultLat = position.coords.latitude;
  19. var defaultLng = position.coords.longitude;
  20. document.getElementById('latitude').value = defaultLat;
  21. document.getElementById('longitude').value = defaultLng;
  22. /*
  23. 1. creates the map
  24. 2. zooms
  25. 3. centers the map
  26. 4. sets the map’s type
  27. */
  28. var map = new google.maps.Map(document.getElementById('canvas'),{
  29. zoom: myZoom,center: new google.maps.LatLng(defaultLat,defaultLng),mapTypeId: google.maps.MapTypeId.ROADMAP
  30. });
  31.  
  32.  
  33. });
  34. // centers the map on markers coords
  35. map.setCenter(myMarker.position);
  36. // adds the marker on the map
  37. myMarker.setMap(map);
  38. }
  39.  
  40. function displayError(error) {
  41. var errors = {
  42. 1: 'Permission denied',2: 'Position unavailable',3: 'Request timeout'
  43. };
  44. alert("Error: " + errors[error.code]);
  45. }
  46. });

上述方法的麻烦在于,很少有用户发现使用起来很困难.几次,他们点击了拒绝,而不是允许,并保持盯着屏幕.所以从可用性的角度来看,我认为一个好办法是:

>请他们许可.
>等待3秒钟,如果他们点击拒绝或不响应,则使用IP在地图上显示地理.

我如何在上面的代码片段中完成第二步.
请让我知道,谢谢!
但是,什么是更好的处理方式

解决方法

这是一个我之前写过的一个脚本( geolocator.js),最近更新了.

更新:Geolocator v2发布.

特征:

> HTML5地理位置(按用户权限)
> IP位置
>地理编码(地址坐标)
>反向地理编码(坐标地址查找)
>完整的地址信息(街道,城镇,邻里,地区,
国家,国家代码,邮政编码等)
回退机制(从HTML5地理位置到IP地理查找)
>观看地理位置
>获取距离矩阵和持续时间
>计算两个地理点之间的距离
>获取时区信息
>获取客户端IP
>支持Google Loader(动态加载Google地图)
>动态创建Google地图(带标记,信息窗口,自动调整缩放)
>非阻塞脚本加载(外部源在运行时加载而不中断页面加载)

live demo.
API documentation.

用法

  1. var options = {
  2. enableHighAccuracy: true,timeout: 6000,maximumAge: 0,desiredAccuracy: 30,// meters
  3. fallbackToIP: true,// get location by IP if geolocation fails or rejected
  4. addressLookup: true,// requires Google API key
  5. timezone: true,// requires Google API key
  6. map: "my-map" // creates a Google map. requires Google API key
  7. };
  8. geolocator.locate(options,function (err,location) {
  9. console.log(err || location);
  10. });

示例输出

  1. {
  2. coords: {
  3. latitude: 37.4224764,longitude: -122.0842499,accuracy: 30,altitude: null,altitudeAccuracy: null,heading: null,speed: null
  4. },address: {
  5. commonName: "",street: "Amphitheatre Pkwy",route: "Amphitheatre Pkwy",streetNumber: "1600",neighborhood: "",town: "",city: "Mountain View",region: "Santa Clara County",state: "California",stateCode: "CA",postalCode: "94043",country: "United States",countryCode: "US"
  6. },formattedAddress: "1600 Amphitheatre Parkway,Mountain View,CA 94043,USA",type: "ROOFTOP",placeId: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",timezone: {
  7. id: "America/Los_Angeles",name: "Pacific Standard Time",abbr: "PST",dstOffset: 0,rawOffset: -28800
  8. },flag: "//cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.3.1/flags/4x3/us.svg",map: {
  9. element: HTMLElement,instance: Object,// google.maps.Map
  10. marker: Object,// google.maps.Marker
  11. infoWindow: Object,// google.maps.InfoWindow
  12. options: Object // map options
  13. },timestamp: 1456795956380
  14. }

猜你在找的HTML5相关文章