首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法使用google-map-react从这个可拖动的标记中获取坐标?

有没有办法使用google-map-react从这个可拖动的标记中获取坐标?
EN

Stack Overflow用户
提问于 2021-08-21 11:05:35
回答 1查看 111关注 0票数 1

我已经做了标记,所以它可以在地图上拖动。不过,我正在尝试从标记中获取坐标,以便将它们保存到我的数据库中。下面是代码,有没有一个函数,当标记被拖动时,我可以从中获得坐标。或者我应该使用另一个google地图react库。

代码语言:javascript
复制
import React, {Component, useCallback} from "react";
import GoogleMapReact from "google-map-react";

class Map extends Component {
  loadMap = (map, maps) => {

    let marker = new maps.Marker({
      position: { lat: 40.856795, lng: -73.954298 },
      map,
      draggable: true,

    });
  };
  

  render() {
    return (
      <div style={{ height: "400px", width: "100%" }}>
        <GoogleMapReact
          bootstrapURLKeys={{ key: "key here" }}
          defaultCenter={{ lat: 40.756795, lng: -73.954298 }}
          defaultZoom={10}
          yesIWantToUseGoogleMapApiInternals
          onGoogleApiLoaded={({ map, maps }) => this.loadMap(map, maps)}

        >
        </GoogleMapReact>
      </div>
    );
  }
}

export default Map;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-21 11:20:29

您将使用google api作为标记,因此可以使用marker docs

具体来说,您应该使用dragend event

就像这样

代码语言:javascript
复制
handleDragEnd = (event) => {
  console.log(event.latLng);
}

loadMap = (map, maps) => {
  let marker = new maps.Marker({
    position: { lat: 40.856795, lng: -73.954298 },
    map,
    draggable: true,
  });
  marker.addListener('dragend', this.handleDragEnd);
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68872443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档