使用了 zepto 和 requirejs
define(['zepto'],function($){ var geolocation = { init:function(config,onSuccess,onError){ if(typeof(config)=='function'){ onError = onSuccess; onSuccess = config; config = undefined; } if(Object(config)===config){ for(var k in 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,返回城市名称 var host = this; navigator.geolocation.getCurrentPosition(function ( position){ console.log('getCurrentPosition success ',position); if(!returnType){ host.onSuccess.call(null,position); }else{ var lat = position.coords.latitude; var lng = position.coords.longitude; var urls="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); } }; return geolocation; });