From fab5985769e2e258ad2dbe83373a6c7aef49b8c6 Mon Sep 17 00:00:00 2001 From: Max Tyler Date: Thu, 21 Jul 2016 23:10:20 +0200 Subject: [PATCH] fix: #118 * remove not loading pokemon spots map * fallback to ip geolcation when device geolocation fails --- src/views/map/index.js | 86 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/views/map/index.js b/src/views/map/index.js index 0d6201f..3545779 100644 --- a/src/views/map/index.js +++ b/src/views/map/index.js @@ -1,9 +1,10 @@ -import { defer } from 'lodash' +import axios from 'axios' import React, { Component } from 'react' import GoogleMap from 'google-map-react' import { observable, action, toJS } from 'mobx' import { observer } from 'mobx-react' +import Alert from 'react-s-alert' import userLocation from '../../models/user-location.js' import settings from '../../models/settings.js' @@ -17,8 +18,6 @@ import TotalDistance from './total-distance.js' import Autopilot from './autopilot.js' import Pokeball from './pokeball.js' -const isLoading = observable(true) - @observer class Map extends Component { @@ -34,44 +33,36 @@ class Map extends Component { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( this.handleGeolocationSuccess, - (error) => console.warn(error), + this.handleGeolocationFail, { enableHighAccuracy: true, maximumAge: 0 } ) } } - @action handleGeolocationSuccess({ coords: { latitude, longitude } }) { - userLocation.replace([ latitude, longitude ]) - } - - @action handleDragMap = ({ zoom: newZoom }) => { - if (newZoom !== settings.zoom.get()) { - settings.zoom.set(newZoom) - - // reset zoom if it has been changed by loading `pokemontSpots.kml` - if (isLoading.get() && newZoom === 3) { - defer(() => { - settings.zoom.set(17) - isLoading.set(false) - }) - } + // geolocation API might be down, use http://ipinfo.io + // source: http://stackoverflow.com/a/32338735 + handleGeolocationFail = async (geolocationErr) => { + Alert.warning(` + Error getting your geolocation, using IP location +
${geolocationErr.message}
+ `, { timeout: 3000 }) + + try { + const { data: { loc } } = await axios({ url: 'http://ipinfo.io/' }) + const [ latitude, longitude ] = loc.split(',').map(coord => parseFloat(coord)) + this.handleGeolocationSuccess({ coords: { latitude, longitude } }) + } catch (xhrErr) { + Alert.error(` + Could not use IP location +
Try to restart app, report issue to github
+
${xhrErr}
+ `) } } - /* eslint max-len: 0 */ - /* eslint no-new: 0 */ - @action handleGoogleMapLoaded = (({ map }) => { - this.map = map - const lastLocation = toJS(userLocation) - - // load pokemon spots map - isLoading.set(true) - const { KmlLayer } = window.google.maps - const url = 'https://gist.githubusercontent.com/iam4x/95e6ab058f1b6dd4fe696d597c2c7e1e/raw/0d2a7356345eccd4e7d397ff535de3ec004ca293/pokemonSpots.kml' - new KmlLayer({ url, map }) - - userLocation.replace(lastLocation) - }) + @action handleGeolocationSuccess({ coords: { latitude, longitude } }) { + userLocation.replace([ latitude, longitude ]) + } @action toggleMapDrag = () => { this.mapOptions.draggable = !this.mapOptions.draggable @@ -79,18 +70,27 @@ class Map extends Component { } render() { + const [ latitude, longitude ] = userLocation + return (
- this.mapOptions } - onGoogleApiLoaded={ this.handleGoogleMapLoaded } - yesIWantToUseGoogleMapApiInternals={ true }> - - + { /* only display google map when user geolocated */ } + { (latitude && longitude) ? + this.mapOptions } + onGoogleApiLoaded={ this.handleGoogleMapLoaded } + yesIWantToUseGoogleMapApiInternals={ true }> + + : +
+ +
}
{ this.mapOptions.draggable ?