高德地图获取当前城市&输入提示
<!doctype html> <html> <head> <meta charset="utf-8"> <title>高德地图获取当前城市&输入提示</title> <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.15&key=943991f38e4aec19029d284ff4ffac9c&plugin=AMap.CitySearch,AMap.Autocomplete,AMap.PlaceSearch"></script> </head> <body> <div id="amapInp"> <input id="tipinput"/> </div> <script type="text/javascript"> //异步方法 Promise.prototype.finally = function (callback) { var P = this.constructor; return this.then( function(value){ P.resolve(callback()).then(function(){ return value }) }, function(reason){ P.resolve(callback()).then(function(){ throw reason }) } ); }; //获取用户所在城市信息 function showCityInfo() { return new Promise(function (resolve, reject) { var res; //实例化城市查询类 var citysearch = new AMap.CitySearch(); //自动获取用户IP,返回当前城市 citysearch.getLocalCity(function(status, result) { if (status === 'complete' && result.info === 'OK') { if (result && result.city && result.bounds) { var cityinfo = result.city; var citybounds = result.bounds; res = cityinfo; } } else { res = result.info; } resolve(res); }); }); } showCityInfo().then(function(res){ console.log('当前城市->',res); //输入提示 var autoOptions = { city: res, input: "tipinput" }; var auto = new AMap.Autocomplete(autoOptions); //构造地点查询类 AMap.event.addListener(auto, "select", select);//注册监听,当选中某条记录时会触发 function select(e) { console.log(e.poi.district,e.poi.address,e.poi.name); } }); </script> </body> </html>