forked from RishikeshVedpathak/ReactNativeMapView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
130 lines (121 loc) · 4.08 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { Component } from 'react';
import { Text, View, ActivityIndicator, Button } from 'react-native';
import MapView from "react-native-maps";
import styles from "./styles";
// Disable yellow box warning messages
console.disableYellowBox = true;
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
region: {
latitude: 10,
longitude: 10,
latitudeDelta: 0.001,
longitudeDelta: 0.001
},
isMapReady: false,
marginTop: 1,
userLocation: "",
regionChangeProgress: false
};
}
componentWillMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
const region = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.001,
longitudeDelta: 0.001
};
this.setState({
region: region,
loading: false,
error: null,
});
},
(error) => {
alert(error);
this.setState({
error: error.message,
loading: false
})
},
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 5000 },
);
}
onMapReady = () => {
this.setState({ isMapReady: true, marginTop: 0 });
}
// Fetch location details as a JOSN from google map API
fetchAddress = () => {
fetch("https://maps.googleapis.com/maps/api/geocode/json?address=" + this.state.region.latitude + "," + this.state.region.longitude + "&key=" + "AIzaSyAXW-WDp0MF5si6oFXaukDQuThTr1wqmDE")
.then((response) => response.json())
.then((responseJson) => {
const userLocation = responseJson.results[0].formatted_address;
this.setState({
userLocation: userLocation,
regionChangeProgress: false
});
});
}
// Update state on region change
onRegionChange = region => {
this.setState({
region,
regionChangeProgress: true
}, () => this.fetchAddress());
}
// Action to be taken after select location button click
onLocationSelect = () => alert(this.state.userLocation);
render() {
if (this.state.loading) {
return (
<View style={styles.spinnerView}>
<ActivityIndicator size="large" color="#0000ff" />
</View>
);
} else {
return (
<View style={styles.container}>
<View style={{ flex: 2 }}>
{!!this.state.region.latitude && !!this.state.region.longitude &&
<MapView
style={{ ...styles.map, marginTop: this.state.marginTop }}
initialRegion={this.state.region}
showsUserLocation={true}
onMapReady={this.onMapReady}
onRegionChangeComplete={this.onRegionChange}
>
{/* <MapView.Marker
coordinate={{ "latitude": this.state.region.latitude, "longitude": this.state.region.longitude }}
title={"Your Location"}
draggable
/> */}
</MapView>
}
<View style={styles.mapMarkerContainer}>
<Text style={{ fontFamily: 'fontawesome', fontSize: 42, color: "#ad1f1f" }}></Text>
</View>
</View>
<View style={styles.deatilSection}>
<Text style={{ fontSize: 16, fontWeight: "bold", fontFamily: "roboto", marginBottom: 20 }}>Move map for location</Text>
<Text style={{ fontSize: 10, color: "#999" }}>LOCATION</Text>
<Text numberOfLines={2} style={{ fontSize: 14, paddingVertical: 10, borderBottomColor: "silver", borderBottomWidth: 0.5 }}>
{!this.state.regionChangeProgress ? this.state.userLocation : "Identifying Location..."}</Text>
<View style={styles.btnContainer}>
<Button
title="PICK THIS LOCATION"
disabled={this.state.regionChangeProgress}
onPress={this.onLocationSelect}
>
</Button>
</View>
</View>
</View>
);
}
}
}