首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用功能组件在google-map-react中向Google地图添加标记

使用功能组件在google-map-react中向Google地图添加标记
EN

Stack Overflow用户
提问于 2020-08-14 00:45:49
回答 2查看 2.1K关注 0票数 0

我正在使用Google包创建一个google-map-react地图和几个标记。坐标、infowindow、图标等信息将来自JSON文件。我添加了onGoogleApiLoaded和yesIWantToUseGoogleMapApiInternals。

这就是我的代码当前的样子,标记暂时在这个文件中

代码语言:javascript
复制
import React, { useEffect } from 'react'
import GoogleMapReact from 'google-map-react'

let mapOptions = {
    center: { lat: 39.56939, lng: -40.0000 },
    zoom: 3.5,
    disableDefaultUI: true,
    gestureHandling: 'none',
    zoomControl: false,
    scaleControl: false,
    zoomControlOptions: false,
    scrollwheel: false,
    panControl: false,
  };
function ModelsMap({ data, state }) {

    console.log(data.models)

    function initMap() {

        let markers = [
         /.../
        ];

        function Marker(props) {

            let marker = new google.maps.marker({
                position: props.coords,
                content: props.content,
                icon: props.icon,
                map: map
            });

            let infoWindow = new google.maps.InfoWindow({
                content: props.content
            });

            Marker.addListener('click', function () {
                infoWindow.open(map, marker);
            })
        }
    }
     // useEffect(initMap, []); it will only render once 
    return (
        <div style={{height: '100vh', width: '100%' }}>
            <GoogleMapReact
                bootstrapURLKeys={{ key: "YOUR_API_KEY" }}
                defaultCenter={{lat: 39.56939, lng: -40.0000 }}
                defaultZoom={3.5}
                options={mapOptions}
                yesIWantToUseGoogleMapApiInternals
                onGoogleApiLoaded={({ map, maps }) => ModelsMap(map, maps)}
                >
                
            </GoogleMapReact>
        </div>
    );
}
export default ModelsMap;

我遵循了This other stack overflow post上列出的解决方案,但也不起作用。

EN

回答 2

Stack Overflow用户

发布于 2020-08-18 06:31:38

根据您提供的StackOverflow的答案,他们使用new maps.Marker()创建了一个新的Google Maps标记。如果你想从json数据中获得坐标、图标和标记的不同属性的值,你需要把这个new maps.Marker()放到每个json数据的循环中。下面应该是您的ModelsMap的值。不要让get导入你的json文件。

代码语言:javascript
复制
const ModelsMap = (map, maps) => {
    //instantiate array that will hold your Json Data
    let dataArray = [];
    //push your Json Data in the array
    {
      data.map((markerJson) => dataArray.push(markerJson));
    }

    //Loop through the dataArray to create a marker per data using the coordinates in the json
    for (let i = 0; i < dataArray.length; i++) {
      let marker = new maps.Marker({
        position: { lat: dataArray[i].lat, lng: dataArray[i].lng },
        map,
        label: dataArray[i].id,
      });
    }
  };

这是一个sample code that shows this。请使用您自己的API密钥才能正常工作。

附注:我删除了您问题中的API密钥。以后请不要在公共网站上分享您的API密钥。

票数 1
EN

Stack Overflow用户

发布于 2021-12-01 12:11:50

从google云控制台启用javascript地图api

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63399688

复制
相关文章

相似问题

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