0

I'm trying to get my position on a map and it works fine on my computer in Google Chrome, but when simulating to android/iPhone nothing happens when using for example Custom Location on iPhone Simulator. Tried it on my actual iPhone as well without any luck. All settings I can find for accepting location on simulator/real device is ON

GoogleMap.js

import React from 'react'
import GoogleMapReact from 'google-map-react'
import {usePosition} from "./usePosition";

const defaultProps = {
    center: {
        lat: 10.0000000,
        lng: 12.0000000,
    },
    zoom: 18.7,
}
const MeOnMap = ({ text }) => <div className="me-on-map"><img src="static/walking.gif" width="30" /></div>
const GoogleMap = () => {
    const { latitude, longitude} = usePosition();
    return (
    <div style={{ marginLeft: '-17px', height: '528px', width: '109%' }}>
        <GoogleMapReact
            bootstrapURLKeys={{ key: '*********' }}
            defaultCenter={defaultProps.center}
            defaultZoom={defaultProps.zoom}
            yesIWantToUseGoogleMapApiInternals
            options={{ scrollwheel: false, zoomControl: false, fullscreenControl: false, gestureHandling: 'none', styles: [{ stylers: [{ 'saturation': 100 }, { 'gamma': 0.5 }]}] }}
        >
            <MeOnMap
                lat={latitude}
                lng={longitude}
                text={''}
            />
        </GoogleMapReact>
    </div>
)}

export default GoogleMap

geo.js

import React from 'react';
import {usePosition} from './usePosition';
export const UsePositions = () => {
    const {latitude, longitude} = usePosition();
    return (
        <code>
            My position,<br/>
            latitude: {latitude}<br/>
            longitude: {longitude}<br/>
        </code>
    );
};

usePosition.js

import {useState, useEffect} from 'react';

const defaultSettings = {
    enableHighAccuracy: false,
    timeout: Infinity,
    maximumAge: 0,
};

export const usePosition = (watch = false, settings = defaultSettings) => {
    const [position, setPosition] = useState({});
    const [error, setError] = useState(null);

    const onChange = ({coords, timestamp}) => {
        setPosition({
            latitude: coords.latitude,
            longitude: coords.longitude,
            accuracy: coords.accuracy,
            speed: coords.speed,
            timestamp,
        });
    };

    const onError = (error) => {
        setError(error.message);
    };

    useEffect(() => {

        if (!navigator || !navigator.geolocation) {
            setError('Geolocation is not supported');
            return;
        }

        let watcher = null;
        if (watch) {
            watcher =
                navigator.geolocation.watchPosition(onChange, onError, settings);
        } else {
            navigator.geolocation.getCurrentPosition(onChange, onError, settings);
        }

        return () => watcher && navigator.geolocation.clearWatch(watcher);

    }, [
        settings.enableHighAccuracy,
        settings.timeout,
        settings.maximumAge,
    ]);

    return {...position, error};
};

Anyone's got any idea what could be wrong? Thanks!!

Michael
  • 1
  • 3

1 Answers1

0

Never mind, it had to do with location being blocked if not gathered from secure connection

Origin does not have permission to use Geolocation service -- even over HTTPS

solved it by running npm with --https

Michael
  • 1
  • 3