php版本
$lng='109.00236';
$lat='34.257034';
$key='写你的key';//记住 web服务类型
$url="http://restapi.amap.com/v3/geocode/regeo?key=.$key.&location=".$lng.','.$lat;
$address=json_decode(file_get_contents($url));
$site=$address->regeocode->formatted_address;
html版本
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>浏览器定位</title>
<script src="http://www.jq22.com/jquery/jquery-2.1.1.js"></script>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
//此处key为javascript类型
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=自己的key"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
<body>
<!--<div id='container'></div> <div id="tip"></div>-->
<script type="text/javascript"> /*************************************** 由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。 ***************************************/ var map, geolocation; //加载地图,调用浏览器定位服务 map = new AMap.Map('container', { resizeEnable: true }); map.plugin('AMap.Geolocation', function() { geolocation = new AMap.Geolocation({ enableHighAccuracy: true,//是否使用高精度定位,默认:true timeout: 10000, //超过10秒后停止定位,默认:无穷大 buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20) zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false buttonPosition:'RB' }); map.addControl(geolocation); geolocation.getCurrentPosition(); AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息 AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息 }); //解析定位成功后 function onComplete(data) { var lng=data.position.getLng(); var lat=data.position.getLat(); $.ajax({ type: "post", url: "http://restapi.amap.com/v3/geocode/regeo", data: { //此处key为web服务型 'key':"写你的key", 'location':lng+','+lat }, dataType: "json", success: function (data) { //控制台打印输出结果 console.log(data); console.log(data.regeocode.addressComponent['city']); }, }); } //解析定位错误信息 function onError(data) { document.getElementById('tip').innerHTML = '定位失败,请重试'; } </script>
</body>
</html>