问题描述
我正在使用 react-google-maps
并且我能够根据 lan
和 lat
显示位置,但是我找不到任何使用共享链接显示位置的方法,例如
https://goo.gl/maps/YBuwmbwH21yUE19Z9
这是我的代码:
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 URLs 在哪里可以创建这样的链接 https://www.google.com/maps/search/?api=1&query=YOURLAT,YOURLNG
这是将在 Google 地图应用中启动坐标的网址。
这是一个 sample code 和代码片段,用于为您在地图上的任何点单击的坐标创建 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;