import React, { PureComponent } from "react";
import {
withGoogleMap,
GoogleMap,
DirectionsRenderer,
withScriptjs,
} from "react-google-maps";
class Map extends PureComponent {
constructor(props) {
super(props);
this.state = {
directions: null,
cords: null,
userFrom: parseFloat(this.props.routing.latitude_from),
userto: parseFloat(this.props.routing.longitude_from),
destinationFrom: parseFloat(this.props.routing.latitude_to),
destinationto: parseFloat(this.props.routing.longitude_to),
};
}
componentDidMount() {
const { userFrom, userto, destinationFrom, destinationto } = this.state;
const directionsService = new window.google.maps.DirectionsService();
const directionsRenderer = new window.google.maps.DirectionsRenderer();
const origin = { lat: userFrom, lng: userto };
const destination = {
lat: destinationFrom,
lng: destinationto,
};
directionsService.route(
{
origin: origin,
destination: destination,
travelMode: window.google.maps.TravelMode.DRIVING,
},
(result, status) => {
directionsRenderer.setDirections({ result });
var route = result.routes[0].legs[0];
const coords = result.routes[0].overview_path;
if (status === window.google.maps.DirectionsStatus.OK) {
this.setState({
directions: result,
distance: route.distance.text,
duration: route.duration.text,
cords: coords,
});
} else {
console.error(error fetching directions ${result}
);
}
}
);
}
render() {
const GoogleMapExample = withGoogleMap((props) => (
<GoogleMap
defaultCenter={{ lat: this.state.userFrom, lng: this.state.userto }}
defaultZoom={10}
>
<DirectionsRenderer
directions={this.state.directions}
onDirectionsChanged={(e) => console.log(e)}
options={{
polylineOptions: {
strokeColor: "#1171ef",
strokeOpacity: 1,
strokeWeight: 4,
},
}}
/>
</GoogleMap>
));
return (
<div>
<GoogleMapExample
containerElement={<div style={{ height: `350px`, width: "100%" }} />}
mapElement={<div style={{ height: `100%` }} />}
></GoogleMapExample>
</div>
);
}
}
const MapLoader = withScriptjs(Map);
export default MapLoader;
{type: "circle", overlay: _.fj}overlay: _.fj {gm_accessors_: {…}, fillColor: "#ffff00", gm_bindings_: {…}, fillOpacity: 1, strokeWeight: 5, …}type: "circle"[[Prototype]]: Object
PolyMap.js?867b:171 _.fj {gm_accessors_: {…}, fillColor: "#ffff00", gm_bindings_: {…}, fillOpacity: 1, strokeWeight: 5, …}Ne: $i {gm_bindings_: {…}, __gm: Aca, gm_accessors_: {…}, renderingType: "RASTER", mapTypeId: "hybrid", …}Vq: (3) [Wf, Wf, Wf]Xe: _.VB {i: div, g: _.WA, j: _.SA, gm_bindings_: {…}, gm_accessors_: {…}, …}center: _.zf {lat: ƒ, lng: ƒ}clickable: falseclosure_uid_543128641: 461draggable_changed: ƒ c()editable: trueeditable_changed: ƒ e()eq: SH {i: _.hj, gm_bindings_: {…}, g: Array(10), __e3_: {…}, ob: _.Hi, …}fillColor: "#ffff00"fillOpacity: 1gm_accessors_: {fillColor: null, fillOpacity: null, strokeWeight: null, clickable: null, editable: null, …}gm_bindings_: {fillColor: {…}, fillOpacity: {…}, strokeWeight: {…}, clickable: {…}, editable: {…}, …}listeners: (11) [Wf, Wf, Wf, Wf, Wf, Wf, Wf, Wf, Wf, Wf, Wf]map: $i {gm_bindings_: {…}, __gm: Aca, gm_accessors_: {…}, renderingType: "RASTER", mapTypeId: "hybrid", …}nr: OH {gm_bindings_: {…}, g: _.SA, gm_accessors_: {…}}radius: 246.17354060977704strokeWeight: 5ti: _.ZB {g: _.kr, gm_accessors_: {…}, strokeColor: "#000000", gm_bindings_: {…}, strokeOpacity: 1, …}un: MH {H: OH, o: KH, i: false, g: _.di, N: ƒ, …}visible: truezIndex: 1__e3_: {toolbar: {…}, mouseover: {…}, click: {…}, mouseout: {…}}[[Prototype]]: _.J
PolyMap.js?867b:171
<GoogleMapReact
bootstrapURLKeys={{
key: '',
}}
defaultCenter={{
lat: 68.970663,
lng: 33.074918
}}
defaultZoom={14}
>
</GoogleMapReact>
import {
GoogleMap,
Marker,
DirectionsRenderer,
Circle,
MarkerClusterer,
useLoadScript
} from "@react-google-maps/api";
import { useState, useMemo, useEffect, useRef } from "react";
import "./Map.css";
const API_KEY = xxxxx;
export default function Map() {
const { isLoaded } = useLoadScript({
id: 'google-map-script',
googleMapsApiKey: API_KEY,
});
if (!isLoaded) return <div> Loading... </div>
return(
<GoogleMap zoom={10} center={{lat:100, lng: 100}} mapContainerClassName="mapContainer" >
<Marker position={{lat:100, lng: 100}}></Marker>
</GoogleMap>
)
}
Hi All,
I want to implement live navigation just like google maps in react (functional). Can anyone please help me how to implement this as i am not getting any idea. I have implemented upto DirectionRender but after that i am not getting any stuffs for this live navigation.
Hello :)
I am using https://react-google-maps-api-docs.netlify.app/#drawingmanager
getting black screen :(
can you help please