📜  获取 google maps getplace lat and long (1)

📅  最后修改于: 2023-12-03 15:27:47.617000             🧑  作者: Mango

获取 Google Maps getPlace 的经纬度

如果您需要在 Google Maps 上使用某个地点的经纬度信息,可以使用 Google Maps API 的 Places 服务来快速获取。下面是一些简单的步骤,向您介绍如何使用 Google Maps API 获取 getPlace 的经纬度。

步骤一:获取 API 密钥

首先,您需要在 Google 开发者控制台上创建一个 API 密钥,并在您的应用程序中使用它。请确保启用了 Places API,并在密钥设置中包含 API 密钥。

更多关于获取 API 密钥的信息请参考 Google Maps Platform 文档

步骤二:请求 getPlace 详细信息

在您的应用程序中,使用以下代码来请求特定地点的详细信息:

var map;
var service;
var infowindow;

function initMap() {
  var sydney = new google.maps.LatLng(-33.867, 151.195);

  map = new google.maps.Map(document.getElementById('map'), {
    center: sydney,
    zoom: 15
  });

  infowindow = new google.maps.InfoWindow();
  service = new google.maps.places.PlacesService(map);
  service.getDetails({
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
  }, callback);
}

function callback(place, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    var lat = place.geometry.location.lat();
    var lng = place.geometry.location.lng();
    console.log(lat, lng);
  }
}

在上述代码中,您需要替换 placeId 参数为您要获取详细信息的地点 ID。您可以在 Google Maps 上查找地点,然后将 ID 复制到上述代码中。

步骤三:获取经纬度信息

以上代码将在 callback 函数中返回地点的详细信息。您可以从中提取地点的经纬度信息。在上述代码示例中,经纬度信息已经存储在 latlng 变量中,并在控制台中输出。

var lat = place.geometry.location.lat();
var lng = place.geometry.location.lng();
console.log(lat, lng);
总结

使用以上步骤,您可以非常简单地获取 getPlace 的经纬度信息,并在您的应用程序中使用。注意,在使用 Google Maps API 时,需要遵守相关的服务条款和隐私政策。

更多关于 Google Maps API 的信息,请参考 Google Maps Platform 文档