Skip to content

Instantly share code, notes, and snippets.

@princessjanf
Last active October 26, 2019 08:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princessjanf/681708cb5f815db052c6fb9980ca8769 to your computer and use it in GitHub Desktop.
Save princessjanf/681708cb5f815db052c6fb9980ca8769 to your computer and use it in GitHub Desktop.
Final Code For React Native's Geolocation
import React, { Component } from "react";
import { AppRegistry, StyleSheet, Dimensions, Image, View, StatusBar, TouchableOpacity } from "react-native";
import { Container, Text } from "native-base";
import MapView from 'react-native-maps';
import Polyline from '@mapbox/polyline';
class LocationA extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
error: null,
concat: null,
coords:[],
x: 'false',
cordLatitude:-6.23,
cordLongitude:106.75,
};
this.mergeLot = this.mergeLot.bind(this);
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
this.mergeLot();
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
}
mergeLot(){
if (this.state.latitude != null && this.state.longitude!=null)
{
let concatLot = this.state.latitude +","+this.state.longitude
this.setState({
concat: concatLot
}, () => {
this.getDirections(concatLot, "-6.270565,106.759550");
});
}
}
async getDirections(startLoc, destinationLoc) {
try {
let resp = await fetch(`https://maps.googleapis.com/maps/api/directions/json?origin=${ startLoc }&destination=${ destinationLoc }`)
let respJson = await resp.json();
let points = Polyline.decode(respJson.routes[0].overview_polyline.points);
let coords = points.map((point, index) => {
return {
latitude : point[0],
longitude : point[1]
}
})
this.setState({coords: coords})
this.setState({x: "true"})
return coords
} catch(error) {
console.log('masuk fungsi')
this.setState({x: "error"})
return error
}
}
render() {
return (
<MapView style={styles.map} initialRegion={{
latitude:-6.270565,
longitude:106.759550,
latitudeDelta: 1,
longitudeDelta: 1
}}>
{!!this.state.latitude && !!this.state.longitude && <MapView.Marker
coordinate={{"latitude":this.state.latitude,"longitude":this.state.longitude}}
title={"Your Location"}
/>}
{!!this.state.cordLatitude && !!this.state.cordLongitude && <MapView.Marker
coordinate={{"latitude":this.state.cordLatitude,"longitude":this.state.cordLongitude}}
title={"Your Destination"}
/>}
{!!this.state.latitude && !!this.state.longitude && this.state.x == 'true' && <MapView.Polyline
coordinates={this.state.coords}
strokeWidth={2}
strokeColor="red"/>
}
{!!this.state.latitude && !!this.state.longitude && this.state.x == 'error' && <MapView.Polyline
coordinates={[
{latitude: this.state.latitude, longitude: this.state.longitude},
{latitude: this.state.cordLatitude, longitude: this.state.cordLongitude},
]}
strokeWidth={2}
strokeColor="red"/>
}
</MapView>
);
}
}
const styles = StyleSheet.create({
map: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});
export default LocationA;
@x5engine
Copy link

new version? amazing work :D

@x5engine
Copy link

import { Container, Text } from "native-base"; are not used here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment