首页 > 解决方案 > 从 Google 地图 URL 获取我的 JSON 的坐标

问题描述

我需要为我的 JSON 从 Google 地图 URL 获取坐标。我从 URL 获取 lat、lng 和 zoom。但是我的标记与谷歌地图标记的位置不同。例如这里: https ://www.google.com/maps/place/ADRA+Laos/@17.8575854,102.1769446,8.81z/data=!4m5!3m4!1s0x3124662d079f33c5:0xcef5f2b499df9a6e!8m2!3d17​​.9613965!4d102.3 我拿 lat: 17.8575854, lng: 102.1769446, zoom: 8.81

我想我错过了一些我必须接受的东西。我的标记与原始标记相差不远,但不在同一个位置。请告诉我我错过了什么

标签: google-maps

解决方案


@ 符号后的坐标是地图的中心,而不是地点的坐标。地点/标记的坐标位于 URL 的末尾:!8m2!3d17.9613965!4d102.6307923

17.9613965,102.6307923

概念证明小提琴

在此处输入图像描述

代码片段:

var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 17.9613965, lng: 102.6307923},
    zoom: 18
  });
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  })
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */
#map {
  height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap" async defer></script>


推荐阅读