I implemented the google map component to view current location and marker drag functionality by using google-maps-react now i want to fill my redux form field with latitude and longitude i tried this way
Location.js
import React, { Component } from "react";
import { Map, InfoWindow, Marker, GoogleApiWrapper } from "google-maps-react";
import { Field, reduxForm, formValueSelector } from "redux-form";
import { connect } from "react-redux";
class Location extends Component {
constructor(props) {
super(props);
this.state = {
lat: null,
lng: null,
markers: []
};
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(position =>
this.setState({
lat: position.coords.latitude,
lng: position.coords.longitude
})
);
}
onMarkerDragEnd = coord => {
const { latLng } = coord;
const lat = latLng.lat();
const lng = latLng.lng();
this.setState({
lat: lat,
lng: lng
});
console.log(lat, lng);
};
render() {
if (!this.props.loaded) {
return <div>Loading...</div>;
}
const style = {
width: "100%",
height: "100vh"
};
return (
<Map
google={this.props.google}
zoom={11}
style={style}
initialCenter={{
lat: this.state.lat,
lng: this.state.lng
}}
center={{
lat: this.state.lat,
lng: this.state.lng
}}
onClick={this.mapClicked}
>
<Marker
title={"Geolocation"}
position={{
lat: this.state.lat,
lng: this.state.lng
}}
draggable={true}
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord)}
/>
</Map>
);
}
}
const selector = formValueSelector("registershop");
const mapStateToProps = state => {
//
//
};
export default GoogleApiWrapper({
apiKey: "......"
})(connect(mapStateToProps)(Location));
Register.JS
import Location from "../common/Location";
class Register extends Component {
const { handleSubmit } = this.props;
return (
<div>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<label>Lat:</label>
<Field name="lat" component="input" type="number" step="0.01" />
<label>Lng:</label>
<Field name="lng" component="input" type="number" step="0.01" />
</form>
<Location/>
<div>
)
}
when user route to the register component i want to show his/her current location for that i used componentDidMount and also i wanted to give chance to user select their location by drag the marker
I'm new to redux form so i don't have better idea for how to bind longitude and latitude to the reduxform to the submit please help me
Related
I am trying to use google-map-react library to locate the user's current location on Google Maps. I managed to use react-geolocated to get coordinates and fed them into the Latitude and Longitude props in the GoogleMapReact component but it was very far from accurate. I am now seeking an alternative on how to use Google Maps API to get more accurate coordinates and focus on the area on a map where the user is located. Using handleApiLoaded method which I failed to understand how to use to achieve this.
Below is some of the code that I wrote to try and solve this
import React from "react";
import GoogleMapReact from "google-map-react";
import { geolocated } from "react-geolocated";
import LocateMeButton from "./Button";
import "./App.css";
const Marker = () => (
<img
src={
"http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/map-marker-icon.png"
}
alt="MyPin"
/>
);
const Map = props => {
const [state, stateUpdater] = React.useState({
lat: "",
lng: "",
text: ""
});
const getGeolocationCordinates = () => {
return !props.isGeolocationAvailable
? stateUpdater({ text: "unsupported browser" })
: !props.isGeolocationEnabled
? stateUpdater({ text: "Geolocation not enabled" })
: props.coords
? stateUpdater({
lng: props.coords.longitude,
lat: props.coords.latitude
})
: "Getting the location";
};
return (
<div className=" map-container">
<LocateMeButton Click={() => getGeolocationCordinates()} />
<GoogleMapReact
bootstrapURLKeys={{
key: "API_KEY",
language: "en"
}}
yesIWantToUseGoogleMapApiInternals
defaultCenter={{ lat: 0.3211264, lng: 32.5910528 }}
defaultZoom={11}
>
<Marker lat={state.lat} lng={state.lng} />
</GoogleMapReact>
</div>
);
};
export default geolocated({
positionOptions: {
enableHighAccuracy: true
},
userDecisionTimeout: 5000
})(Map);
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<!-- end snippet -->
import React from "react";
import GoogleMapReact from "google-map-react";
import { geolocated } from "react-geolocated";
import LocateMeButton from "./Button";
import "./App.css";
const Marker = () => (
<img
src={
"http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/map-marker-icon.png"
}
alt="MyPin"
/>
);
const Map = props => {
const [state, stateUpdater] = React.useState({
lat: "",
lng: "",
text: ""
});
const getGeolocationCordinates = () => {
return !props.isGeolocationAvailable
? stateUpdater({ text: "unsupported browser" })
: !props.isGeolocationEnabled
? stateUpdater({ text: "Geolocation not enabled" })
: props.coords
? stateUpdater({
lng: props.coords.longitude,
lat: props.coords.latitude
})
: "Getting the location";
};
return (
<div className=" map-container">
<LocateMeButton Click={() => getGeolocationCordinates()} />
<GoogleMapReact
bootstrapURLKeys={{
key: "AIzaSyBtgM1mf0N-Pcsko7Dc5Q2La-K460a9IsA",
language: "en"
}}
yesIWantToUseGoogleMapApiInternals
defaultCenter={{ lat: 0.3211264, lng: 32.5910528 }}
defaultZoom={11}
>
<Marker lat={state.lat} lng={state.lng} />
</GoogleMapReact>
</div>
);
};
export default geolocated({
positionOptions: {
enableHighAccuracy: true
},
userDecisionTimeout: 5000
})(Map);
I removed the API key that you included in your question. In the future, make sure that you won't post your API key in a public site to protect it.
You can implement this use case using your browser's HTML5 Geolocation feature along with the Maps JavaScript API.
Here is a sample code and code snippet below that implements this in google-map-react library.
import React, { Component } from "react";
import GoogleMapReact from "google-map-react";
import "./style.css";
class GoogleMaps extends Component {
constructor(props) {
super(props);
this.state = {
currentLocation: { lat: 40.756795, lng: -73.954298 }
};
}
render() {
console.log(this.state.inputRad);
const apiIsLoaded = (map, maps) => {
navigator?.geolocation.getCurrentPosition(
({ coords: { latitude: lat, longitude: lng } }) => {
const pos = { lat, lng };
this.setState({ currentLocation: pos });
}
);
};
return (
<div>
<div style={{ height: "400px", width: "100%" }}>
<GoogleMapReact
bootstrapURLKeys={{
key: "YOUR_API_KEY",
libraries: ["places"]
}}
defaultCenter={{ lat: 40.756795, lng: -73.954298 }}
defaultZoom={10}
center={this.state.currentLocation}
yesIWantToUseGoogleMapApiInternals
onGoogleApiLoaded={({ map, maps }) => apiIsLoaded(map, maps)}
/>
</div>
</div>
);
}
}
export default GoogleMaps;
Hi folks I'm using the react-google-maps library. I'm trying to recenter my map (zoom where the marker is) every time my location changes, but I'm getting a bit lost on how to implement the whole thing. I can see the marker being updated, but the map stays on its defaultCenter position.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
GoogleMap,
Marker,
withScriptjs,
withGoogleMap
} from 'react-google-maps';
import environment from '../../config/environment';
class Map extends Component {
static propTypes = {
defaultZoom: PropTypes.number,
center: PropTypes.shape({
lat: PropTypes.number,
lng: PropTypes.number
}),
location: PropTypes.shape({
lat: PropTypes.number,
lng: PropTypes.number
}),
onPositionChanged: PropTypes.func
};
static defaultProps = {
defaultZoom: 14,
center: {
lat: 60.1699,
lng: 24.9384
},
location: {},
onPositionChanged: () => {}
};
constructor(props) {
super(props);
this.mapRef = React.createRef((ref) => {
this.mapRef = ref;
});
}
componenDidUpdate() {
console.log(`I'm about to update with props: ${JSON.strongify(prevProps, undefined, 2)}`);
}
onPositionChanged = (location) => {
console.log(`This the new location onPositionChange:${JSON.stringify(location, undefined, 2)}`);
const newLocation = new window.google.maps.LatLng(location.lat, location.lng);
// [NOTE]: try using the panTo() from googleMaps to recenter the map ? but don't know how to call it.
return (
<Marker
position={newLocation}
/>
);
}
render() {
const {
center,
defaultZoom,
location,
onPositionChanged
} = this.props;
return (
<GoogleMap
className="google-map"
onClick={onPositionChanged(location)}
defaultZoom={defaultZoom}
defaultCenter={center}
ref={this.mapRef}
>
{/* <Marker
position={location}
/> */}
{ this.onPositionChanged(location) }
</GoogleMap>
);
}
}
const SchedulerGoogleMap = withScriptjs(withGoogleMap(Map));
const SchedulerMap = props => (
<SchedulerGoogleMap
googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${
environment.GOOGLE_MAPS_API_KEY
}&v=3`}
loadingElement={<div style={{ height: '20vh' }} />}
containerElement={<div style={{ height: '100%' }} />}
mapElement={<div style={{ height: '20vh', width: '100%' }} />}
{...props}
/>
);
export { Map, SchedulerMap, SchedulerGoogleMap };
Simply pass the center prop to your GoogleMap component instead of the defaultCenter prop. The center prop is mutable whereas defaultZoom is not.
This is what it seemed to work for me, just in case any other person runs into the same problem.
... ommited_code
class Map extends Component {
... ommited_code
componentDidUpdate(prevProps) {
if (prevProps.location !== this.props.location) {
this.mapRef.panTo(
new window.google.maps.LatLng(this.props.location.lat, this.props.location.lng)
);
}
}
render() {
const {
center,
defaultZoom,
location
} = this.props;
return (
<GoogleMap
className="google-map"
defaultZoom={defaultZoom}
defaultCenter={center}
ref={(ref) => {
this.mapRef = ref;
}}
>
<Marker position={new window.google.maps.LatLng(location.lat, location.lng)} />
</GoogleMap>
);
}
}
...ommited_code
panTo() is a method of the google.maps.Map class. (https://developers.google.com/maps/documentation/javascript/reference/map#Map.panTo)
It seems to be the function you are looking for, so you need to call it on your google map by referencing the className you set for your map, then give the panTo method the LatLang object you created:
window.google.maps.Map(document.getElementsByClassName("google-map")).panTo(newLocation);
I'm trying to show Google Map with centering the map based on latitude and longitude which are returned by Geolocation. However, the map shows as the default value and not get rendered by Geolocation values. I set latitude and longitude in component state and trying to re-render the component after the state is updated. But it does not work. Below is my code.
MapView.js
import React, { Component } from 'react'
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps'
import MapComponent from './MapComponent'
class MapView extends Component {
constructor(props){
super(props)
this.state = {
currentLatLng: {
lat: 0,
lng: 0
},
isMarkerShown: false
}
}
componentWillUpdate(){
this.getGeoLocation()
}
componentDidMount() {
this.delayedShowMarker()
}
delayedShowMarker = () => {
setTimeout(() => {
this.getGeoLocation()
this.setState({ isMarkerShown: true })
}, 5000)
}
handleMarkerClick = () => {
this.setState({ isMarkerShown: false })
this.delayedShowMarker()
}
getGeoLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
this.setState({
lat: position.coords.latitude,
lng: position.coords.longitude
})
}
)
} else {
error => console.log(error)
}
}
render() {
return (
<MapComponent
isMarkerShown={this.state.isMarkerShown}
onMarkerClick={this.handleMarkerClick}
currentLocation={this.state.currentLatLng}
/>
)
}
}
export default MapView;
MapComponent.js
import React, { Component } from 'react'
import { compose, withProps } from 'recompose'
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps'
const MapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap
)((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: props.currentLocation.lat, lng: props.currentLocation.lng }}
>
{props.isMarkerShown && <Marker position={{ lat: props.currentLocation.lat, lng: props.currentLocation.lng }} onClick={props.onMarkerClick} />}
</GoogleMap>
)
export default MapComponent
In fact map is not centered since currentLatLng is not getting updated, you might want something like this:
getGeoLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
console.log(position.coords);
this.setState(prevState => ({
currentLatLng: {
...prevState.currentLatLng,
lat: position.coords.latitude,
lng: position.coords.longitude
}
}))
}
)
} else {
error => console.log(error)
}
}
instead of original getGeoLocation function
According to w3.org, the current location can be obtained with:
function showMap(position) {
// Show a map centered at (position.coords.latitude, position.coords.longitude).
console.log(position.coords.latitude);
console.log(position.coords.longitude);
}
// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);
This links Geolocation API:
Geolocation API w3.org
This worked for me, location high accuracy.
I'm trying to get the current position on google map. The code is working fine when I get the coordinates, but these coordinates I want to get and google map toom because right now, coordinates are static for google map.
import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
import { withGoogleMap, GoogleMap, Marker } from "google-map-react"
import { InfoWindow } from 'google-map-react';
import Geolocation from 'react-geolocation'
const AnyReactComponent = ({ text }) => <div>{ text }</div>;
export default class Map extends Component {
static defaultProps = {
center: { lat: 40.7446790, lng: -73.9485420 },
zoom: 11
}
render() {
return (
<Geolocation
render={({
fetchingPosition,
position: { coords: { latitude, longitude } = {} } = {},
error,
getCurrentPosition
}) =>
<div>
<button onClick={getCurrentPosition}>Get Position</button>
{error &&
<div>
{error.message}
</div>}
<pre>
latitude: {latitude}
longitude: {longitude}
</pre>
<div className='google-map' style={{ height: '80vh', width: '100%' }}>
<GoogleMapReact
defaultCenter={ this.props.center }
defaultZoom={ this.props.zoom }>
<AnyReactComponent
lat={ latitude }
lng={ longitude }
text={ 'Wheres Waldo?' }
/>
</GoogleMapReact>
</div>
</div>}
/>
)
}
}
With Geolocation I get very well coordinates, but these coordinates want to set in:
static defaultProps = {
center: { lat: 40.7446790, lng: -73.9485420 },
zoom: 11
}
You must define a state for your <Map> component, which will store your current coordinates.
Then, you should pass the state to your GoogleMapReact component.
In order to set the state with the coordinates found by the Geolocation services, you can use onSuccess callback (https://www.npmjs.com/package/react-geolocation#onsuccess-function):
<Geolocation
onSuccess={this.geoSuccess}
...
/>
The geoSuccess function is:
geoSuccess = position => {
let coords = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
this.setState({
center: coords
})
};
Demo here:
https://43qv620770.codesandbox.io/
The full code can be found here (the file Map.js):
https://codesandbox.io/s/43qv620770
I am trying to open a infowindow on a specific marker on click, however when i click one it appears that all of them are opening instead, and showing me this error:
React.Children.only expected to receive a single React element child.
This is what my code looks like right now:
import React, { Component } from 'react';
import { GoogleMap, Marker, withGoogleMap, withScriptjs, InfoWindow } from 'react-google-maps'
class Map extends Component {
state = {
isOpen: false
}
handleMarkerClick = () => {
this.setState({ isOpen: true})
}
render() {
return(
<div>
<GoogleMap
defaultZoom={13}
defaultCenter={{ lat: -22.9034778, lng: -43.1264636 }}
>{this.props.markers.map((marker, index) =>(
<Marker
key={index}
position={marker.location}
onClick={this.handleMarkerClick}
>{this.state.isOpen && <InfoWindow onCloseClick={this.handleMarkerClick}/>}
</Marker>
))}
</GoogleMap>
</div>
)
}
}
export default withScriptjs(withGoogleMap(Map))
Start of Edit
I made some changes to try and address the comment, however it isn't working yet, can you give me some hints on what i'm doing wrong, since i made some changes to the top component i will paste it here too:
import React, { Component } from 'react';
import Map from './Map.js'
import List from './List.js'
import escapeRegExp from 'escape-string-regexp'
import sortBy from 'sort-by'
class App extends Component {
state ={
locations:[
{
name: "Paróquia Nossa Senhora das Dores do Ingá",
location: {
lat: -22.9038875,
lng: -43.1252873
},
isOpen:false,
},
{
name: "Museu de Arte Contemporanea de Niteroi",
location: {
lat: -22.9078182,
lng: -43.1262919
},
isOpen:false,
},
{
name: "UFF - Faculdade de Direito",
location: {
lat: -22.9038469,
lng: -43.126024
},
isOpen:false,
},
{
name: "Ponte Rio-Niterói",
location: {
lat: -22.8701,
lng: -43.167
},
isOpen:false,
},
{
name: "Fundação Oscar Niemeyer",
location: {
lat: -22.888533927137285,
lng: -43.12815992250511
},
isOpen:false,
},
{
name: "Campo de São Bento",
location: {
lat: -22.905279,
lng: -43.107759
},
isOpen:false,
}
],
query:''
}
onToggleOpen = (location) => {
this.setState({ isOpen: !this.isOpen })
}
updateQuery = (query) => {
this.setState({ query: query.trim() })
console.log(query)
}
componentDidMount() {}
render() {
const { query, locations } = this.state
let filteredMarkers
if(query) {
const match = new RegExp(escapeRegExp(query), 'i')
filteredMarkers = locations.filter((location) => match.test(location.name))
}else {
filteredMarkers = locations
}
filteredMarkers.sort(sortBy('name'))
return (
<div className="App">
<div style={{height:`5vh`}}>
<input
type='text'
placeholder='Search locations'
value={query}
onChange={(event) => this.updateQuery(event.target.value)}
/>
</div>
<List
markers={filteredMarkers}
/>
<Map
onToggle={this.onToggleOpen}
googleMapURL="https://maps.googleapis.com/maps/api/js?&key=AIzaSyAiqO5W1p5FAFf8RZD11PGigUXSlmVHguQ&v=3"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `80vh` }} />}
mapElement={<div style={{ height: `100%` }} />}
className="Map"
markers={filteredMarkers}
/>
</div>
);
}
}
export default App;
Map.js
import React, { Component } from 'react';
import { GoogleMap, Marker, withGoogleMap, withScriptjs, InfoWindow } from 'react-google-maps'
class Map extends Component {
render() {
return(
<div>
<GoogleMap
defaultZoom={13}
defaultCenter={{ lat: -22.9034778, lng: -43.1264636 }}
>{this.props.markers.map((marker, index) =>(
<Marker
key={index}
position={marker.location}
onClick={() => this.props.onToggle(marker)}
>{marker.isOpen && <InfoWindow onCloseClick={this.ontoggleOpen}>Hello</InfoWindow>}
</Marker>
))}
</GoogleMap>
</div>
)
}
}
export default withScriptjs(withGoogleMap(Map))
The problem i was having with React.Children.only expected to receive a single React element child. was being caused because i didn't set a div inside the infowindow, so simply by adding it this particular problem was solved.
Here is what it used to look like:
<InfoWindow onCloseClick={this.handleMarkerClick}/>
here is what it should look like:
<InfoWindow onCloseClick={()=>this.props.onToggle(marker)}><div>Hello</div></InfoWindow>
or something along these lines.