非常感谢您 thinhvo0108 ,了解如何使用 google-map-react 。他的回答可以在这里找到。


 然而,虽然这确实解决了我的问题,但我原来的问题仍然存在。想象一下,没有创建反应组件消除了使用< script>
的麻烦,我将如何使用脚本实现Google Maps,如下面的问题所示。换句话说,如何使用React(ES6 / ES7)
< script> 的形式实现纯javascript脚本>我正在努力在我的网络应用程序中添加Google地图。我发现了这个伟大的发电机这里,它给了我这段代码:
&# xA;
 <!DOCTYPE html>
< html>
< head>
 < meta charset =“UTF-8”>
 < title> Google Maps< / title>
< / head>
< body>
 < div id =“map_canvas”style =“width:500px; height:300px;”>< / div>

 < script src =“https://maps.google.com/maps/api/js”>< / script>
 <脚本>
 window.onload = function(){
初始化();
 };

 function initialize(){
 var myLatlng = new google.maps.LatLng(50.3419169876,5.60793614352);

 var myOptions = {
 zoom:16,
 center:myLatlng,
 mapTypeId:google.maps.MapTypeId.ROADMAP
 };

 var map = new google.maps.Map(document.getElementById(“map_canvas”),myOptions);

 var infowindow = new google.maps.InfoWindow({
 content:“Someplace”
});

 var marker = new google.maps.Marker({
 position:myLatlng,
 map:map,
 title:“Someplace”
});
&#xA ; google.maps.event.addListener(marker,'click',function(){
 infowindow.open(map,marker);
});
 }
 < / script>
< / body>
< / html>



 所以我拿了脚本并将其粘贴到我的index.html文件中。 Google地图实例应该放在联系页面上。显然当我重新加载页面/ contact时,我会快速加载地图。但是,由于窗口超出了反应范围,每当我使用导航栏导航时,它都不起作用。


我有这个仅包含div的GoogleMaps.js组件id map_canvas:


 import来自'react'的反应

 const GoogleMapsInstance =()=> (
< div id =“map_canvas”style = {{width:'100%',height:'400px',overflow:'hidden'}} />
)
 export默认GoogleMapsInstance



 我想知道如何在这个组件中获取脚本,所以我可以调用componentDidMount函数上的脚本或类似的东西。


我也对任何其他类型的解决方案开放,它让我快速加载(无iframe)谷歌地图:)

&# xA;感谢您的帮助!

答案 0 :(得分:2)
这种方式并不复杂,根据您的喜好,我昨天刚发布并且有效!
Implementing google maps with react
如果您想将此地图元素添加到您的组件中,反之亦然,请告诉我们!
以下版本(根据您的需要):
您可以按照本指南使地图显示在您的联系页面上:
首先,安装必要的库:
npm install --save google-map-react
npm install --save prop-types
然后,您可以复制以下内容:
import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
const AnyReactComponent = ({ text }) => <div>{text}</div>;
class MyClass extends Component {
constructor(props){
super(props);
}
render() {
return (
<GoogleMapReact
defaultCenter={this.props.center}
defaultZoom={this.props.zoom}
style={{width: '500px', height: '300px'}}
>
<AnyReactComponent
lat={50.3419169876}
lng={5.60793614352}
text={'MY SHOP'}
/>
</GoogleMapReact>
);
}
}
MyClass.defaultProps = {
center: {lat: 50.34, lng: 5.61},
zoom: 16
};
export default MyClass;
随意编辑LatLng值,和/或&#34; MY SHOP&#34;文本,我相信您也可以使用一些自定义PNG图像而不是text-element来显示代表您商店的首选地图引脚
答案 1 :(得分:0)
在Google上进行简单搜索;)