Methods Introduction:
1. This method is a promise object because either GPS or with high moral positioning are asynchronous
2. API methods comes with h5 priority is GPS positioning, if the position will not get to use High German localization (German high positioning requires high moral key)
3. The package is very humble little promise, ability so far, will gradually optimized, the normal method originally used to add callback process to obtain the latitude and longitude, but think twice or use the promise can be a good point
note:
1. Now many browsers requires encryption protocol in order to obtain a link to the user's location, be safe, use an encrypted protocol link
2. Use high moral need to apply for key positioning in Germany, where high
3. If you need high precision or even subsequent high moral map of longitude and latitude to do other processing, you need to get to the latitude and longitude gps turn into a high moral latitude and longitude, the head of the project attracted GPS2AMAP is to deal with this, the code below
1 / * 2 of the method is to expose a promise, the latitude and longitude of the first parameter resolve in 3 Usage: startLocation.then (({lat, lng }) => { }, this is your method) 4 * / . 5 . 6 // the German gps latitude longitude to a high, the high precision required thing . 7 Import { . 8 the gPS . 9 } from './GPS2AMAP' 10 the let the callback = null // Resolve promise be assigned to the function 11 / / GPS positioning is successful callback 12 is const the onSuccess = (position) => { 13 is const { 14 Latitude: LAT, 15 longitude: LNG 16 } = position.coords . 17 const = Exchange GPS.gcj_encrypt (Number.parseFloat (LAT), Number.parseFloat (LNG)) 18 is the console.log ( 'GPS positioning', `$ {exchange.lat} - $ {}` exchange.lng) . 19 saveLocation (exchange.lat, exchange.lng) 20 is } 21 is // GPS location failure callback 22 is const the onError = () => { 23 is initAMAP () 24 console.error ( 'GPS failed' ) 25 } 26 // initialization high German, high load de JS 27 const initAMAP = () => { 28 const = Key 'XXXXXXXXXXX', // bought from high German Key 29 the MP =new Promise(function (resolve, reject) { 30 window.init = function () { 31 resolve(AMap) 32 }; 33 let script = document.createElement("script"); 34 script.type = "text/javascript"; 35 script.src = "//webapi.amap.com/maps?v=1.4.6&key="+key+"&callback=init"; 36 script.onerror = reject; 37 document.head.appendChild(script); 38 }) 39 MP.then(function(Amap) { 40 startAMAPLocation (amap) 41 is }). The catch (ERR => { 42 is console.error (the JSON.stringify (ERR)); 43 is }) 44 is } 45 46 is // Start Location high German 47 const startAMAPLocation = ( amap) => { 48 AMap.plugin ( 'AMap.Geolocation', function () { 49 var Geolocation = new new AMap.Geolocation ({ 50 enableHighAccuracy: to true , // whether to use the high-precision positioning, default: to true 51 is timeout: 8000 , //More than 10 seconds after the stop position, default: 5S 52 is buttonPosition: 'the RB', // docked position location button 53 is buttonOffset: new new AMap.Pixel (10, 20 is), // the parked position of the navigation buttons provided offset default: Pixel (10, 20 is) 54 is zoomToAccuracy: to true , // positioning is automatically adjusted to the setpoint successfully map view 55 }); 56 is // Map.addControl (Geolocation); 57 is geolocation.getCurrentPosition ( function (Status, Result) { 58 IF (Status == 'Complete' ) { 59 the console.log ( 'high German:' , result.position.lat} {$ `,` $ {} result.position.lng) 60 saveLocation (result.position.lat, result.position.lng) 61 is } the else { 62 is console.error ( 'high failure de' ) 63 is saveLocation (0, 0 ) 64 } 65 }); 66 }); 67 } 68 / / process the acquired latitude and longitude 69 const saveLocation = (LAT = 0, LNG = 0) => { 70 LAT && (LAT = parseFloat (LAT) .toFixed (. 6 )) 71 is LNG && (= parseFloat LNG (LNG) .toFixed (. 6 )) 72 typeof the callback === 'function' && the callback ({LAT, LNG}) 73 is } 74 75 // Promise, the first parameter is resolve latitude, 76 const = StartLocation new new Promise ((resolve, Reject) => { 77 the callback = resolve 78 const Options = { 79 enableHighAccuracy: to true , 80 maximumAge: 1000 , 81 timeout: 5000 82 } 83 IF (navigator.geolocation) { 84 navigator.geolocation.getCurrentPosition (the onSuccess, the onError, Options) 85 } the else { 86 saveLocation (0, 0 ) 87 console.error ( 'Your browser does not support the geo-targeting' ) 88 } 89 }) 90 Export default StartLocation
Here GPS2AMAP.js
. 1 Export the GPS const = { 2 the PI: 3.14159265358979324 , . 3 x_pi: 3.14159265358979324 * 3000.0 / 180.0 , . 4 Delta: function (LAT, LNG) { . 5 var A = 6,378,245.0; // A: ellipsoidal coordinates satellite map coordinates projected to the plane the projection factor. . 6 var EE = 0.00669342162296594323; // EE: eccentricity of the ellipsoid. . 7 var dlat = the this .transformLat (LNG - 105.0, LAT - 35.0 ); . 8 var dlng = the this .transformlng (LNG - 105.0, LAT - 35.0 ); . 9 var radLat = lat / 180.0 * this.PI; 10 var magic = Math.sin(radLat); 11 magic = 1 - ee * magic * magic; 12 var sqrtMagic = Math.sqrt(magic); 13 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI); 14 dlng = (dlng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI); 15 return {'lat': dLat, 'lng': dlng}; 16 }, 17 18 //GPS---高德 19 gcj_encrypt : function(WgsLat, wgslng) { 20 if ( this .outOfChina (wgsLat, wgslng)) 21 return { 'lat': wgsLat, 'u' : wgslng}; 22 23 var d = this .delta (wgsLat, wgslng); 24 return { 'lat': wgsLat + d.lat, 'u': wgslng + d.lng}; 25 }, 26 outOfChina: function (lat, lng) { 27 if (i <72.004 || i> 137.8347 ) 28 return true ; 29 if (lat <0.8293 || lat> 55.8271 ) 30 return true; 31 return false; 32 }, 33 transformLat : function (x, y) { 34 var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); 35 ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0; 36 ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0; 37 ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0; 38 return ret; 39 }, 40 transformlng : function (x, y) { 41 var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); 42 ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0; 43 ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0; 44 ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0; 45 return ret; 46 } 47 };
the above