Zepto and Requirejs are used
define ([' Zepto '),function($){ varGeolocation ={init:function(config,onsuccess,onerror) {if(typeof(config) = = ' function ') {OnError=onsuccess; Onsuccess=config; Config=undefined; } if(Object (config) = = = =config) { for(varKinch This. Option) { This. option[k] =Config[k]; } } if(typeof(onsuccess) = = ' function '){ This. onsuccess =onsuccess; } if(typeof(onError) = = ' function '){ This. OnError =OnError; } return This; }, GetPosition:function(ReturnType) {//Returntype==cityname, return city name varHost = This; Navigator.geolocation.getCurrentPosition (function(position) {Console.log (' GetCurrentPosition success ', position); if(!returntype) {Host.onSuccess.call (NULL, position); }Else{ varLat =Position.coords.latitude; varLNG =Position.coords.longitude; varUrls= "http://api.map.baidu.com/geocoder/v2/?ak=0qobgmq4i5n0fycrcqmukgqd&callback=renderreverse& Location= "+lat+", "+lng+" 8&output=json&pois=0 "; $.ajax ({type:"POST", Url:urls, DataType:"Jsonp", Success:function(data) {//Alert (json.stringify (data)); //alert (host.onsuccess)Console.log (' Get City name>>> ', data); Host.onSuccess.call (NULL, data); }, Error:function(Error) {Host.onError.call (NULL, error); } }); } }, function(Error) {Host.onError.call (NULL, error); }, This. option); return This; }, option:{enablehighaccuracy:true, Maximumage:30000, timeout:20000}, Onsuccess:function(data) {Console.log (' Geolocation.getcurrentposition success: ', data); }, OnError:function(Error) {Console.log (' Geolocation.getcurrentposition error: ', error); } };returngeolocation; });
Use HTML5 geolocation technology and Baidu Map API to query the current location