简体   繁体   中英

How to display shared link in google map react

I am using react-google-maps and I am able to display location based on lan and lat but I can not find any way to display location using shared link for example https://goo.gl/maps/YBuwmbwH21yUE19Z9

I want to display this link in google map

Here is my code:

  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>
  );
};

Please help me, Thanks you

Please note that react-google-maps is a library created for Maps JavaScript API which is a product of Google Maps Platform. Google Maps Platform is different entity from Google Maps App.

If you would like to provide a link that will launch to Google Maps App using the latLng you have in your code, you can try to check Maps URLs where you can make a link like this https://www.google.com/maps/search/?api=1&query=YOURLAT,YOURLNG which is a URL that will launch the coordinate in the Google Maps App.

Here is a sample code and code snippet that creates a Google Maps Link for the coordinates you click at any points in the map:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM