[英]How to display shared link in google map react
我正在使用react-google-maps
,我能够根据lan
和lat
显示位置,但我找不到任何使用共享链接显示位置的方法,例如https://goo.gl/maps/YBuwmbwH21yUE19Z9
我想在谷歌 map 中显示这个链接
这是我的代码:
withGoogleMap(props => (
<GoogleMap
defaultZoom={17}
defaultCenter={{ lat: props.lat, lng: props.lng }}
>
{props.isMarkerShown && (
<Marker
clickable={true}
title={'asdasdads'}
position={{ lat: props.lat, lng: props.lng }}
/>
)}
</GoogleMap>
)),
);
const Location = ({ URL }) => {
var splitUrl = URL.split('!3d');
var latLong = splitUrl[splitUrl.length - 1].split('!4d');
var longitude;
if (latLong.indexOf('?') !== -1) {
longitude = latLong[1].split('\\?')[0];
} else {
longitude = latLong[1];
}
var latitude = latLong[0];
return (
<div
className={`box-dashboard d-flex justify-content-center align-items-center bg-white mb-4`}
>
<MyMapComponent
lat={parseFloat(latitude)}
lng={parseFloat(longitude)}
isMarkerShown
googleMapURL={`https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=${YOUR_GOOGLE_API_KEY}`}
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `300px`, width: '100%' }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
);
};
请帮助我,谢谢
请注意, react-google-maps
是为 Google Maps Platform 的产品 Maps JavaScript API 创建的库。 Google Maps Platform 与 Google Maps App 不同。
如果您想使用代码中的 latLng 提供将启动到 Google Maps App 的链接,您可以尝试检查Maps URL ,您可以在其中创建这样的链接https://www.google.com/maps/search/?api=1&query=YOURLAT,YOURLNG
这是一个 URL,它将在 Google 地图应用程序中启动坐标。
这是一个示例代码和代码片段,它为您在 map 中的任何点单击的坐标创建一个 Google 地图链接:
import React, { Component } from "react";
import { withGoogleMap, GoogleMap } from "react-google-maps";
class Map extends Component {
constructor(props) {
super(props);
this.state = {
center: { lat: 24.886, lng: -70.268 },
link: ""
};
}
onMapClick = e => {
console.log(e.latLng.lat());
this.setState({
link:
"https://www.google.com/maps/search/?api=1&query=" +
e.latLng.lat() +
"," +
e.latLng.lng()
});
};
render() {
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
defaultCenter={this.state.center}
defaultZoom={3}
onClick={this.onMapClick}
/>
));
return (
<div>
<p>Click the map to get link of coordinate</p>
{this.state.link !== "" && (
<a href={this.state.link}>{this.state.link}</a>
)}
<GoogleMapExample
containerElement={<div style={{ height: `500px`, width: "500px" }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
);
}
}
export default Map;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.