HTML5 获取用户坐标方法

 1 function GetLocation() {
 2             if (navigator.geolocation) {
 3                 var locationOptions = {
 4                     //是否使用高精度设备,如GPS。默认是true
 5                     enableHighAccuracy: true,
 6                     //超时时间,单位毫秒,默认为0
 7                     timeout: 5000,
 8                     //使用设置时间内的缓存数据,单位毫秒
 9                     //默认为0,即始终请求新数据
10                     //如设为Infinity,则始终使用缓存数据
11                     maximumAge: 0
12                 };
13                 //只执行一次
14                 navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
15 
16                 //只要设备位置发生变化,就会执行
17                 //watcherID = navigator.geolocation.watchPosition(locationSuccess, locationError, locationOptions);
18 
19                 //用于终止watchPosition方法
20                 //navigator.geolocation.clearWatch(watcher_id);
21             } else {
22                 $.jBox.info("Your browser does not support Geolocation!");
23             }
24         }
25 
26 function locationError(error) {
27             switch (error.code) {
28                 case error.TIMEOUT:
29                     $.jBox.info("A timeout occured! Please try again!");
30                     break;
31                 case error.POSITION_UNAVAILABLE:
32                     $.jBox.info('We can\'t detect your location. Sorry!');
33                     break;
34                 case error.PERMISSION_DENIED:
35                     $.jBox.info('Please allow geolocation access for this to work.');
36                     break;
37                 case error.UNKNOWN_ERROR:
38                     $.jBox.info('An unknown error occured!');
39                     break;
40             }
41         }
42 
43 function locationSuccess(position) {
44             var coords = position.coords;
45              //coords.latitude;
46              //coords.longitude;
47         }    

反向地址解析

 

 1 var geoCoder = new google.maps.Geocoder();
 2 
 3 var point = new google.maps.LatLng(lat, lng);
 4 
 5 geoCoder.geocode({ 'latLng': point }, function (results, state) {
 6                 if (state = google.maps.GeocoderStatus.OK) {
 7                     if (results[0]) {
 8                         var point = results[0].geometry.location;
 9                         var myDirection = results[0].formatted_address;
10 
11                         myMarker = new google.maps.Marker({
12                             position: point,
13                             map: map,
14                             title: "You Are Here"
15                         });
16 
17                         var infowindow = new google.maps.InfoWindow({
18                             content: myDirection
19                         });
20 
21              google.maps.event.addListener(myMarker, 'click', function() {
22                         infowindow.open(map, myMarker);
23             });
24                     }
25                 }
26             });
27 
28             google.maps.event.addListener(map, "rightclick", function(event) {
29                 var lat = event.latLng.lat();
30                 var lng = event.latLng.lng();
31             });

 

转载于:https://www.cnblogs.com/TandyChan/p/4919740.html

你可能感兴趣的:(HTML5 获取用户坐标方法)