Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove react-google-maps dependency #1389

Merged
merged 6 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ way to update this template, but currently, we follow a pattern:

## Upcoming version 2020-XX-XX

- [change] Remove react-google-maps dependency. It has not been maintained for 3 years. From now on,
we use Google Maps API directly. However, the default map provider is still Mapbox.
[#1389](https://github.com/sharetribe/ftw-daily/pull/1389)
- [fix] Pass metadata through sanitizeUser function.
[#1391](https://github.com/sharetribe/ftw-daily/pull/1391)
- [fix] Call for the same page caused unnecessary rendering
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"react-dom": "^16.13.1",
"react-final-form": "^6.5.1",
"react-final-form-arrays": "^3.1.2",
"react-google-maps": "^9.4.5",
"react-helmet-async": "^1.0.6",
"react-intl": "^3.1.13",
"react-moment-proptypes": "^1.6.0",
Expand Down Expand Up @@ -76,7 +75,6 @@
},
"resolutions": {
"react-dates/lodash": "^4.17.19",
"react-google-maps/lodash": "^4.17.19",
"react-test-renderer": "^16.13.1",
"handlebars": "^4.5.3",
"serialize-javascript": "^2.1.1"
Expand Down
150 changes: 95 additions & 55 deletions src/components/Map/DynamicGoogleMap.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
import React from 'react';
import React, { Component } from 'react';
import { number, object, shape, string } from 'prop-types';
import { withGoogleMap, GoogleMap, Marker, Circle } from 'react-google-maps';
import classNames from 'classnames';
import { circlePolyline } from '../../util/maps';
import config from '../../config';

/**
* DynamicGoogleMap uses withGoogleMap HOC.
* It handles some of the google map initialization states.
* DynamicGoogleMap uses Google Maps API.
*/
const DynamicGoogleMap = withGoogleMap(props => {
const { center, zoom, address, mapsConfig } = props;

const { enabled, url, anchorX, anchorY, width, height } = mapsConfig.customMarker;
const markerIcon = enabled
? {
icon: {
url,

// The origin for this image is (0, 0).
origin: new window.google.maps.Point(0, 0),
size: new window.google.maps.Size(width, height),
anchor: new window.google.maps.Point(anchorX, anchorY),
},
}
: {};

const marker = <Marker position={center} {...markerIcon} title={address} />;

const circleProps = {
options: {
fillColor: mapsConfig.fuzzy.circleColor,
fillOpacity: 0.2,
strokeColor: mapsConfig.fuzzy.circleColor,
strokeOpacity: 0.5,
strokeWeight: 1,
clickable: false,
},
radius: mapsConfig.fuzzy.offset,
center,
};

const circle = <Circle {...circleProps} />;

const controlPosition =
typeof window !== 'undefined' && typeof window.google !== 'undefined'
? window.google.maps.ControlPosition.LEFT_TOP
: 5;

return (
<GoogleMap
defaultZoom={zoom}
defaultCenter={center}
center={center}
options={{
class DynamicGoogleMap extends Component {
constructor(props) {
super(props);
this.map = null;
this.mapContainer = null;

this.initializeMap = this.initializeMap.bind(this);
}

componentDidMount(prevProps) {
if (!this.map && this.mapContainer) {
this.initializeMap();
}
}

initializeMap() {
const { offsetHeight, offsetWidth } = this.mapContainer;
const hasDimensions = offsetHeight > 0 && offsetWidth > 0;

if (hasDimensions) {
const { center, zoom, address, mapsConfig } = this.props;
const { enabled, url, anchorX, anchorY, width, height } = mapsConfig.customMarker;
const maps = window.google.maps;
const controlPosition = window.google.maps.ControlPosition.LEFT_TOP;

const mapConfig = {
center,
zoom,
// Disable all controls except zoom
// https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions

// Disable map type (ie. Satellite etc.)
mapTypeControl: false,
// Disable zooming by scrolling
Expand All @@ -64,12 +50,66 @@ const DynamicGoogleMap = withGoogleMap(props => {
zoomControlOptions: {
position: controlPosition,
},
}}
>
{mapsConfig.fuzzy.enabled ? circle : marker}
</GoogleMap>
);
});
};

this.map = new maps.Map(this.mapContainer, mapConfig);

if (mapsConfig.fuzzy.enabled) {
const GoogleLatLng = window.google.maps.LatLng;
// Origin as object literal (LatLngLiteral)
const origin = { lat: center.lat, lng: center.lng };
const radius = mapsConfig.fuzzy.offset;
const path = circlePolyline(origin, radius).map(c => new GoogleLatLng(c[0], c[1]));

const circleProps = {
options: {
fillColor: mapsConfig.fuzzy.circleColor,
fillOpacity: 0.2,
strokeColor: mapsConfig.fuzzy.circleColor,
strokeOpacity: 0.5,
strokeWeight: 1,
clickable: false,
},
path,
map: this.map,
};

// Add a circle. We use Polygon because the default Circle class is not round enough.
const Polygon = window.google.maps.Polygon;
new Polygon(circleProps);
} else {
const markerIcon = enabled
? {
icon: {
url,

// The origin for this image is (0, 0).
origin: new window.google.maps.Point(0, 0),
size: new window.google.maps.Size(width, height),
anchor: new window.google.maps.Point(anchorX, anchorY),
},
}
: {};

const marker = new window.google.maps.Marker({
position: center,
map: this.map,
title: address,
...markerIcon,
});
}
}
}

render() {
const { containerClassName, mapClassName } = this.props;
return (
<div className={containerClassName}>
<div className={mapClassName} ref={el => (this.mapContainer = el)} />
</div>
);
}
}

DynamicGoogleMap.defaultProps = {
address: '',
Expand Down
2 changes: 0 additions & 2 deletions src/components/Map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export class Map extends Component {
<StaticMap center={location} zoom={zoom} address={address} mapsConfig={mapsConfig} />
) : (
<DynamicMap
containerElement={<div className={classes} />}
mapElement={<div className={mapClasses} />}
containerClassName={classes}
mapClassName={mapClasses}
center={location}
Expand Down
2 changes: 0 additions & 2 deletions src/components/ResponsiveImage/ResponsiveImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const ResponsiveImage = props => {
if (image == null || variants.length === 0) {
const noImageClasses = classNames(rootClassName || css.root, css.noImageContainer, className);

// NoImageMessage is needed for listing images on top the map (those component lose context)
// https://github.com/tomchentw/react-google-maps/issues/578
const noImageMessageText = noImageMessage || <FormattedMessage id="ResponsiveImage.noImage" />;
return (
<div className={noImageClasses}>
Expand Down
18 changes: 11 additions & 7 deletions src/components/SearchMap/SearchMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class SearchMapComponent extends Component {

render() {
const {
id,
className,
rootClassName,
reusableContainerClassName,
Expand Down Expand Up @@ -145,11 +146,9 @@ export class SearchMapComponent extends Component {
// When changing from default map provider to Google Maps, you should use the following
// component instead of SearchMapWithMapbox:
//
// <SearchMapWithGoogleMap
// containerElement={
// <div id="search-map-container" className={classes} onClick={this.onMapClicked} />
// }
// mapElement={<div className={mapRootClassName || css.mapRoot} />}
// <SearchMapWithGoogleMaps
Gnito marked this conversation as resolved.
Show resolved Hide resolved
// id={id}
// className={classes}
// bounds={bounds}
// center={center}
// location={location}
Expand All @@ -158,10 +157,12 @@ export class SearchMapComponent extends Component {
// activeListingId={activeListingId}
// mapComponentRefreshToken={this.state.mapReattachmentCount}
// createURLToListing={this.createURLToListing}
// onClick={this.onMapClicked}
// onListingClicked={this.onListingClicked}
// onListingInfoCardClicked={this.onListingInfoCardClicked}
// onMapLoad={this.onMapLoadHandler}
// onMapMoveEnd={onMapMoveEnd}
// reusableMapHiddenHandle={REUSABLE_MAP_HIDDEN_HANDLE}
// zoom={zoom}
// />

Expand All @@ -173,6 +174,7 @@ export class SearchMapComponent extends Component {
messages={messages}
>
<SearchMapWithMapbox
id={id}
className={classes}
bounds={bounds}
center={center}
Expand All @@ -182,13 +184,13 @@ export class SearchMapComponent extends Component {
activeListingId={activeListingId}
mapComponentRefreshToken={this.state.mapReattachmentCount}
createURLToListing={this.createURLToListing}
onClick={this.onMapClicked}
onListingClicked={this.onListingClicked}
onListingInfoCardClicked={this.onListingInfoCardClicked}
onMapLoad={this.onMapLoadHandler}
onClick={this.onMapClicked}
onMapMoveEnd={onMapMoveEnd}
zoom={zoom}
reusableMapHiddenHandle={REUSABLE_MAP_HIDDEN_HANDLE}
zoom={zoom}
Gnito marked this conversation as resolved.
Show resolved Hide resolved
/>
</ReusableMapContainer>
) : (
Expand All @@ -198,6 +200,7 @@ export class SearchMapComponent extends Component {
}

SearchMapComponent.defaultProps = {
id: 'searchMap',
className: null,
rootClassName: null,
mapRootClassName: null,
Expand All @@ -212,6 +215,7 @@ SearchMapComponent.defaultProps = {
};

SearchMapComponent.propTypes = {
id: string,
className: string,
rootClassName: string,
mapRootClassName: string,
Expand Down
Loading