Nyx*_*nyx 22 javascript google-maps google-maps-api-3 meteor reactjs
我正在使用google-map-reactNPM包创建一个Google Map和几个标记.
问题:我们如何将默认的Google地图标记添加到地图中?
从这个Github问题看来,我们似乎需要使用onGoogleApiLoaded函数访问内部Google Maps API .
参考Google Maps JS API文档中的示例,我们可以使用以下代码来呈现标记,但是我们如何定义对它的引用map?
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
Run Code Online (Sandbox Code Playgroud)
现行代码:
renderMarkers() {
...
}
render() {
return (
<div style={{'width':800,'height':800}}>
<GoogleMap
bootstrapURLKeys={{key: settings.googleMapApiKey}}
defaultZoom={13}
defaultCenter={{
lat: this.props.user.profile.location.coordinates[1],
lng: this.props.user.profile.location.coordinates[0]
}}
onGoogleApiLoaded={({map, maps}) => this.renderMarkers()}
yesIWantToUseGoogleMapApiInternals
>
</GoogleMap>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
Mas*_*rAM 19
从自述文件中的描述可能并不完全清楚,但maps实际上,论证是地图API对象(map当然是当前的Google Map实例).因此,您应该将两者都传递给您的方法:
onGoogleApiLoaded={({map, maps}) => this.renderMarkers(map, maps)}
Run Code Online (Sandbox Code Playgroud)
并使用它们:
renderMarkers(map, maps) {
let marker = new maps.Marker({
position: myLatLng,
map,
title: 'Hello World!'
});
}
Run Code Online (Sandbox Code Playgroud)
Job*_*uel 10
在地图上添加标记并不像我们想的那么容易,主要是因为令人困惑的文档,但在这里你有一个非常简单的例子:
const Map = props => {
return (
<GoogleMapReact
bootstrapURLKeys={{ props.key }}
defaultCenter={{lat: props.lat, lng: props.lng}}
defaultZoom={props.zoom}>
{/* This is the missing part in docs:
*
* Basically, you only need to add a Child Component that
* takes 'lat' and 'lng' Props. And that Component should
* returns a text, image, super-awesome-pin (aka, your marker).
*
*/}
<Marker lat={props.lat} lng={props.lng}} />
</GoogleMapReact>
)
}
const Marker = props => {
return <div className="SuperAwesomePin"></div>
}
Run Code Online (Sandbox Code Playgroud)
import React from 'react';
import GoogleMapReact from 'google-map-react';
const GoogleMaps = ({ latitude, longitude }) => {
const renderMarkers = (map, maps) => {
let marker = new maps.Marker({
position: { lat: latitude, lng: longitude },
map,
title: 'Hello World!'
});
return marker;
};
return (
<div style={{ height: '50vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: 'YOUR KEY' }}
defaultCenter={{ lat: latitude, lng: longitude }}
defaultZoom={16}
yesIWantToUseGoogleMapApiInternals
onGoogleApiLoaded={({ map, maps }) => renderMarkers(map, maps)}
>
</GoogleMapReact>
</div>
);
};
export default GoogleMaps;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16818 次 |
| 最近记录: |