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

[Feature] Location Google Maps view #4390

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
98 changes: 96 additions & 2 deletions fields/types/location/LocationField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash';
import React from 'react';
import { withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
import withScriptjs from 'react-google-maps/lib/async/withScriptjs';
import Field from '../Field';
import CollapsedFieldLabel from '../../components/CollapsedFieldLabel';
import NestedFormField from '../../components/NestedFormField';
Expand All @@ -12,6 +14,25 @@ import {
LabelledControl,
} from '../../../admin/client/App/elemental';

const AsyncGoogleMap = _.flowRight(
withScriptjs,
withGoogleMap,
)(props => (
<GoogleMap
ref={props.onMapLoad}
defaultZoom={16}
defaultCenter={props.defaultCenter}
onClick={props.onMapClick}
>
{props.marker && (
<Marker
{...props.marker}
onRightClick={() => props.onMarkerRightClick(props.marker)}
/>
)}
</GoogleMap>
));

/**
* TODO:
* - Remove dependency on underscore
Expand All @@ -34,9 +55,10 @@ module.exports = Field.create({
},

componentWillMount () {
const { value = [] } = this.props;
const { map, value = [] } = this.props;
const fields = map ? [] : ['number', 'name', 'street2', 'geo'];
var collapsedFields = {};
_.forEach(['number', 'name', 'street2', 'geo'], (i) => {
_.forEach(fields, (i) => {
if (!value[i]) {
collapsedFields[i] = true;
}
Expand Down Expand Up @@ -177,6 +199,77 @@ module.exports = Field.create({
);
},

handleMapLoad (map) {
this.map = map;
},

// Remove marker
handleMarkerRightClick () {
const { value = {}, path, onChange } = this.props;
const geo = ['', ''];
onChange({
path,
value: {
...value,
geo,
},
});
},

handleMapClick (event) {
const { value = {}, path, onChange } = this.props;
const lng = event.latLng.lng();
const lat = event.latLng.lat();
const geo = [
lng || (value.geo ? value.geo[0] : 0.0),
lat || (value.geo ? value.geo[1] : 0.0),
];
onChange({
path,
value: {
...value,
geo,
},
});
},

renderMap () {
const { value = {}, map, height, browserApiKey, defaultCenter } = this.props;
if (!map) return null;
const hasLng = value && value.geo && value.geo[0];
const hasLat = value && value.geo && value.geo[1];
const marker = hasLng && hasLat ? {
position: {
lng: value.geo[0],
lat: value.geo[1],
},
} : null;

const googleMapURL = `https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=${browserApiKey}`;

return (
<FormField offsetAbsentLabel>
<AsyncGoogleMap
googleMapURL={googleMapURL}
defaultCenter={marker ? marker.position : defaultCenter}
loadingElement={
<div style={{ height }} />
}
containerElement={
<div style={{ height }} />
}
mapElement={
<div style={{ height }} />
}
marker={marker}
onMarkerRightClick={this.handleMarkerRightClick}
onMapLoad={this.handleMapLoad}
onMapClick={this.handleMapClick}
/>
</FormField>
);
},

renderGeo () {
if (this.state.collapsedFields.geo) {
return null;
Expand Down Expand Up @@ -282,6 +375,7 @@ module.exports = Field.create({
{this.renderSuburbState()}
{this.renderPostcodeCountry()}
{this.renderGeo()}
{this.renderMap()}
{this.renderGoogleOptions()}
{this.renderNote()}
</div>
Expand Down
10 changes: 9 additions & 1 deletion fields/types/location/LocationType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var utils = require('keystone-utils');

var RADIUS_KM = 6371;
var RADIUS_MILES = 3959;
var DEFAULT_CENTER = { lat: -25.363882, lng: 131.044922 };

/**
* Location FieldType Constructor
Expand All @@ -16,7 +17,7 @@ function location (list, path, options) {

this._underscoreMethods = ['format', 'googleLookup', 'kmFrom', 'milesFrom'];
this._fixedSize = 'full';
this._properties = ['enableMapsAPI'];
this._properties = ['enableMapsAPI', 'map', 'height', 'browserApiKey', 'defaultCenter'];
this.enableMapsAPI = (options.enableImprove === true || (options.enableImprove !== false && keystone.get('google server api key'))) ? true : false;

// Throw on invalid options in 4.0 (remove for 5.0)
Expand Down Expand Up @@ -44,6 +45,13 @@ function location (list, path, options) {
if (!this.requiredPaths) {
this.requiredPaths = ['street1', 'suburb'];
}
// Should display as a Google Map
this.map = options.map || false;
if (this.map) {
this.browserApiKey = keystone.get('google api key');
this.height = options.height || 300;
this.defaultCenter = options.defaultCenter || DEFAULT_CENTER;
}

location.super_.call(this, list, path, options);
}
Expand Down
18 changes: 18 additions & 0 deletions fields/types/location/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ Google Places integration requires the `google api key` option to be set for Key

`geo` `Array` longitude, latitude

`map` `Boolean` - Show Google Map view and select coordinates by clicking on it. Right click removes the mark. Requires the `google api key`.

```js
{ type: Types.Location, map: true }
```

`defaultCenter` `Object` - Center Google Map view at this point when `geo` value is missing.

```js
{ type: Types.Location, defaultCenter: { lat: -25.363882, lng: 131.044922 } }
```

`height` `Number` - Show Google Map view height. By default it is `300`.

```js
{ type: Types.Location, map: true, height: 400 }
```

> Important: as per the MongoDB convention, the order for the geo array must be lng, lat which is the opposite of the order used by Google's API.

## Underscore methods
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"react-dnd-html5-backend": "2.2.4",
"react-dom": "15.4.2",
"react-domify": "0.2.6",
"react-google-maps": "6.3.0",
"react-images": "0.5.2",
"react-markdown": "2.4.5",
"react-redux": "5.0.3",
Expand Down