Reactjs 在谷歌地图中显示我的当前位置图标

Reactjs 在谷歌地图中显示我的当前位置图标,reactjs,google-maps,react-google-maps,Reactjs,Google Maps,React Google Maps,我们有没有办法通过使用谷歌地图显示谷歌地图中的当前位置图标 我在文档中找不到任何显示当前位置图标的道具。 这是我的组件,我只需要显示用户当前位置图标 const googleMapCard= compose( withProps({ googleMapURL: "https://maps.googleapis.com/maps/api/js?key=-----------, loadingElement: <div style={{ height: `1

我们有没有办法通过使用谷歌地图显示谷歌地图中的当前位置图标 我在文档中找不到任何显示当前位置图标的道具。 这是我的组件,我只需要显示用户当前位置图标

  const googleMapCard= compose(
  withProps({
    googleMapURL:
      "https://maps.googleapis.com/maps/api/js?key=-----------,
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `400px` }} />,
    mapElement: <div style={{ height: `100%` }} />
  }),
  lifecycle({
    componentWillMount() {
      const refs = {};

      this.setState({
        bounds: null,
        markers: [],
        onMapMounted: ref => {
          refs.map = ref;
        },
        onBoundsChanged: () => {
          this.setState({
            bounds: refs.map.getBounds(),
            center: refs.map.getCenter()
          });
        },
        onSearchBoxMounted: ref => {
          refs.searchBox = ref;
        },
        onPlacesChanged: onChange => {
          const places = refs.searchBox.getPlaces();
          const bounds = new window.google.maps.LatLngBounds();
        }
      });
    }
  }),
  withScriptjs,
  withGoogleMap
)(props => (
  <GoogleMap
    onClick={e => {
      props.onChange([e.latLng.lng(), e.latLng.lat()]);
    }}
    clickableIcons={true}
    defaultZoom={8}
    center={{ lat: props.value[1], lng: props.value[0] }}
    defaultCenter={{ lat: props.value[1], lng: props.value[0] }}
    ref={props.onMapMounted}
  >
    <SearchBox
      ref={props.onSearchBoxMounted}
      // bounds={props.bounds}
      controlPosition={window.google.maps.ControlPosition.TOP_LEFT}
      onPlacesChanged={() => {
        props.onPlacesChanged(props.onChange);
      }}
    >
      <input
        type="text"
      />
    </SearchBox>
    <Marker position={{ lat: props.value[1], lng: props.value[0] }} />
    )}
  </GoogleMap>
));
const googleMapCard=compose(
用道具({
谷歌地图网址:
"https://maps.googleapis.com/maps/api/js?key=-----------,
加载元素:,
集装箱运输:,
mapElement:
}),
生命周期({
组件willmount(){
常量refs={};
这是我的国家({
界限:空,
标记:[],
onMapMounted:ref=>{
refs.map=ref;
},
onBoundsChanged:()=>{
这是我的国家({
边界:refs.map.getBounds(),
中心:refs.map.getCenter()
});
},
onSearchBoxMounted:ref=>{
refs.searchBox=ref;
},
onPlacesChanged:onChange=>{
const places=refs.searchBox.getPlaces();
const bounds=new window.google.maps.LatLngBounds();
}
});
}
}),
用ScriptJS,
用谷歌地图
)(道具=>(
{
props.onChange([e.latLng.lng(),e.latLng.lat()]);
}}
clickableIcons={true}
defaultZoom={8}
中心={{lat:props.value[1],lng:props.value[0]}
defaultCenter={{lat:props.value[1],lng:props.value[0]}
ref={props.onMapMounted}
>
{
道具更换(道具更换);
}}
>
)}
));

您可以使用地理定位api。 见:


它与react google maps无关,它是一种本机浏览器api,使用本机地理定位api编写导航安全坐标访问的更简单方法

navigator?.geolocation.getCurrentPosition(({coords: {latitude: lat, longitude: lng}}) => {
    const pos = {lat, lng}
    this.setState({currentPosition: pos})  
})
它使用:

  • ?。
    空访问检查-
  • 解构
  • 别名
  • =>
    lambda箭头
navigator?.geolocation.getCurrentPosition(({coords: {latitude: lat, longitude: lng}}) => {
    const pos = {lat, lng}
    this.setState({currentPosition: pos})  
})