在javascript new google.maps.LatLng中划分ID

时间:2017-12-16 19:02:43

标签: javascript php jquery

这是我使用谷歌地图从位置检测ip的代码问题是我如何设置new google.maps.LatLng('< HERE >') to <div id="loc"></div>?结果是 40.4652,-74.2307 #loc

的一部分

<script>
function myMap() {
var mapProp= {
     center: new google.maps.LatLng('<div id="loc"></div>'),
    zoom:5,
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
</script>

<Script>
$.get("https://ipinfo.io/104.238.132.170/json", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#loc").html("" + response.loc);
    $("#region").html("" + response.region);
    $("#country").html("" + response.country);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
</script>
<div id="googleMap" style="width:100%;height:400px;"></div>

1 个答案:

答案 0 :(得分:0)

您可以提取纬度经度

 [lat,lng] = $("#loc").html().split(',').map(parseFloat);

并将它们传递给google.maps.LatLng

var mapProp= {
   center: new google.maps.LatLng(lat, lng)
相关问题