question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get google's map object

See original GitHub issue

Here is my component:

'use strict';

import React from 'react';
import {withGoogleMap, GoogleMap} from 'react-google-maps';
import {MAP} from 'react-google-maps/lib/constants';

export default class Presentation extends React.Component {
    
    static contextTypes = {
        [MAP]: React.PropTypes.object
    }
    
    render() {
        
        var _this = this;

        const MyMap = withGoogleMap((props) => (
            <GoogleMap
                defaultCenter: {
                    lat: 38.809972,
                    lng: -100.60729
                },
                defaultZoom: 5,
                onDragEnd={(e) => {
                    console.log(_this.context[MAP]);
                    console.log(e.latLng.lat(), e.latLng.lng());
                }}
            ></GoogleMap>
        ));

        return (
            <MyMap
                loadingElement={<div className="map-loading" />}
                containerElement={<div className="map-container" />}
                mapElement={<div className="map-element" />}
            />
        );

    }

}

In onDragEnd function I want to get google’s map object. Here I found an example how to do that: https://github.com/tomchentw/react-google-maps/issues/161 (I mean using context) But _this.context[MAP] is always undefined for me.

Please provide any workable example or just tell me what is wrong with mine.

  • “react”: “^15.4.0”
  • “react-google-maps”: “^9.4.5”

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
ivgrincommented, Feb 23, 2018

Yes, this works for me too, big thanks!!)

1reaction
marwan38commented, Feb 23, 2018
<GoogleMap ref={map => this._map = map} />
onDragEnd={this._map.getCenter().toJSON()}

I’ve been trying to figure out how to update the current center coords, this is what worked for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages | Maps JavaScript API - Google Developers
You may have supplied an incorrect version number in your script element. Please make sure you are using a correct version number. See...
Read more >
Get google.maps.Map instance from a HTMLElement
You can't get google.maps.Map object from DOM Element ,on which Google Maps object have been constructed. google.maps.Map is just a wrapper, ...
Read more >
@react-google-maps/api - npm
If you want to use window.google object, you need to extract GoogleMap in separate module, so it is lazy executed then google-maps-api script...
Read more >
Google Maps not working on your iPhone? 12 Real fixes
You might also find this fix helpful if Google Maps fails to launch or ... Google Maps accuracy as it lets you scan...
Read more >
How to fix Maps when it crashes - Android - Google Maps Help
Clear the app's cache & data · On your Android phone or tablet, open the Settings app . · Tap Apps & notifications....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found