0

i am trying to get the current location of a user but everytime i run my emulator it is showing me the location of googleplex this is the output iam getting .My guess is that its the default location somehow. I added "ACCESS_FINE_LOCATION" in my manifest.xml and here is my source code . . . . . . .

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    Dimensions
} from 'react-native';

// import { Actions } from 'react-native-router-flux';
import MapView from 'react-native-maps'

const { width, height } = Dimensions.get('window')
const SCREEN_HEIGHT = height
const SCREEN_WIDTH = width
const ASPECT_RATIO = width / height
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;

export default class Location extends Component {
    constructor(props){
        super(props);
        this.state = {
            initialPosition: {
              latitude: 0,
              longitude: 0,
              latitudeDelta: 0,
              longitudeDelta: 0
            },
            markerPosition: {
              latitude: 0,
              longitude: 0,

            }
        }
    }
    watchID: ?number = null
    componentDidMount(){
        navigator.geolocation.getCurrentPosition((position) => {
            //let lat = parseFloat(position.coords.latitude);
            //let long = parseFloat(position.coords.longitude);
            var lat = parseFloat(position.coords.latitude);
            var long = parseFloat(position.coords.longitude);

            //this.setState({initialRegion : {
              var initialRegion = {
                latitude : lat,
                longitude : long,
                latitudeDelta : LATITUDE_DELTA,
                longitudeDelta: LONGITUDE_DELTA
            }
              this.setState({initialPosition: initialRegion})
              this.setState({markerPosition: initialRegion})


        },
            (error)=> alert(JSON.stringify(error)),
            {enableHighAccuracy : true , timeout: 20000, maximumAge:1000})

            this.watchID= navigator.geolocation.watchPosition((position)=>{
              var lat = parseFloat(position.coords.latitude)
              var long = parseFloat(position.coords.longitude)

              var lastRegion ={
                latitude: lat,
                longitude: long,
                latitudeDelta: LATITUDE_DELTA,
                longitudeDelta: LONGITUDE_DELTA
              }
              this.setState({initialPosition: lastRegion})
              this.setState({markerPosition: lastRegion})
            })
    }
    componentWillUnmount(){
      navigator.geolocation.clearWatch(this.watchID)
    }

    render(){
        return(
            <View style={styles.container}>
              <MapView style={styles.map}
              region ={this.state.initialPosition}>

              <MapView.Marker
              coordinate={this.state.markerPosition}
              >
              <View style={styles.radius}>
              <View style={styles.marker}>
              </View>
              </View>
              </MapView.Marker>
              </MapView>

            </View>
        );
    }}


    const styles = StyleSheet.create({
      container: {
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        justifyContent: 'flex-end',
        alignItems: 'center',
      },
      map: {
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
      },
    });

Also if someone could help me, add the current location button it would be very helpful.

Taha J
  • 71
  • 1
  • 3
  • so does this mean i have to change it everytime i run my code ? – Taha J Apr 12 '18 at 12:34
  • If you want to test out different locations, yes, you need to manually change the location to move around in the emulator. If you don't want to do that, you can either script a route using the command line interface on Android, use one of the moving location settings on iOS, or use a real device to test on (you'd get your real location in this case). – Michael Cheng Apr 12 '18 at 12:38

0 Answers0