【问题标题】:Google Maps Places API - how to get name of a place? [duplicate]Google Maps Places API - 如何获取地名? [复制]
【发布时间】:2020-12-28 14:05:45
【问题描述】:

当我在地图上更改指针时,我想获取一个地点的名称。 我能够在地图上获得一个地方的纬度和经度,但我想获得那个地方的名称。例如 lat: 43.669992353311635, lng: -79.4108552 是多伦多。我的代码有什么错误?

库:react-google-maps

  onDragEnd: ({ setDrag, setCenter, setUseMyLocation, onChangeMapCoords  }) => () => {
    const center = refs.map.getCenter();
    const placeName = refs.map.getPlaces();
    console.log("ACTION sp8 map center", placeName)
    // console.log("ACTION sp8 map center", { lat: center.lat(), lng: center.lng()})

    setUseMyLocation(false);
    setDrag(false);
    setCenter(({ lat: center.lat(), lng: center.lng() }));
    resetSearchBox();
  },

【问题讨论】:

标签: reactjs google-maps


【解决方案1】:

你可以这样使用

 val geocoder = Geocoder(requireContext(), Locale.getDefault())
  // parameter 1 is the number of results you want, a maximum of 5
 val addresses: List<Address> = geocoder.getFromLocation(lat, lng, 1)

【讨论】:

    【解决方案2】:

    您可以使用Geocoding service 对您在DragEnd 获得的纬度进行反向地理编码。这样,您就可以从反向地理编码中获取地址的名称。

    下面是sample code和Geocoding services的代码sn-p:

      onDragEnd(e) {
        console.log(e.latLng);
        const geocoder = new google.maps.Geocoder();
        let loc = e.latLng;
        geocoder.geocode({ location: loc }, (results, status) => {
          if (status === "OK") {
            if (results[0]) {
              console.log(results[0].formatted_address);
            } else {
              window.alert("No results found");
            }
          } else {
            window.alert("Geocoder failed due to: " + status);
          }
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-12
      • 2014-08-26
      • 2011-06-30
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      相关资源
      最近更新 更多