<script>
function findPosition(){
var address = "位置信息";
var url = "https://restapi.amap.com/v3/geocode/geo?address=$"+address+"&output=JSON&key=自己申请的KEY";
var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象
httpRequest.open('GET', url, true);//第二步:打开连接 将请求参数写在url中 ps:"./Ptest.php?name=test&nameone=testone"
httpRequest.send();//第三步:发送请求 将请求参数写在URL中
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText;//获取到json字符串,还需解析
var obj = JSON.parse(json)
var json2 = JSON.stringify(obj.geocodes);
var obj2 = JSON.parse(json2);
document.getElementById("longitude").value = obj2[0].location.split(",")[0];
document.getElementById("latitude").value = obj2[0].location.split(",")[1];
}
};
}
</script>
JS根据获取的位置信息查询相应的经纬度
最新推荐文章于 2024-06-26 08:42:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)