引入高德地图插件
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=??">
function showCityInfo() {
//加载城市查询插件
AMap.service(["AMap.CitySearch"], function() {
//实例化城市查询类
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;
city = cityinfo;
citycode = cityinfo.replace("市","");
$('#market option:contains("'+citycode +'")').attr('selected','selected');
// console.log("您当前所在城市:" + cityinfo + ",code:"+ cityinfo.replace("市",""));
console.log(cityinfo);
}
}else{
console.log("您当前所在城市:" + result.info + "");
}
});
});
}
showCityInfo();
然后就能获取到当前所在的位置 信息了。