@@ -4,23 +4,32 @@ import {
44 View ,
55 Text ,
66 Dimensions ,
7+ Platform ,
8+ PermissionsAndroid ,
79} from 'react-native' ;
810
911import MapView , { MAP_TYPES , PROVIDER_DEFAULT } from 'react-native-maps' ;
1012
13+ const geoViewport = require ( '@mapbox/geo-viewport' ) ;
14+ const tilebelt = require ( '@mapbox/tilebelt' ) ;
15+
1116const { width, height } = Dimensions . get ( 'window' ) ;
1217
1318const ASPECT_RATIO = width / height ;
14- const LATITUDE = 37.78825 ;
15- const LONGITUDE = - 122.4324 ;
16- const LATITUDE_DELTA = 0.0922 ;
19+ const LATITUDE = - 8.380882 ;
20+ const LONGITUDE = - 74.448166 ;
21+ const LATITUDE_DELTA = 0.2222 ;
1722const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO ;
1823
1924class CustomTiles extends React . Component {
2025 constructor ( props , context ) {
2126 super ( props , context ) ;
2227
2328 this . state = {
29+ coordinates : {
30+ tile : [ ] , // tile coordinates x, y, z + precision x, y
31+ precision : [ ] , // tile precision x, y
32+ } ,
2433 region : {
2534 latitude : LATITUDE ,
2635 longitude : LONGITUDE ,
@@ -36,20 +45,95 @@ class CustomTiles extends React.Component {
3645 MAP_TYPES . STANDARD : MAP_TYPES . NONE ;
3746 }
3847
48+ componentDidMount ( ) {
49+ if ( Platform . OS === 'android' ) {
50+ PermissionsAndroid . request ( PermissionsAndroid . PERMISSIONS . READ_EXTERNAL_STORAGE ) ;
51+ PermissionsAndroid . request ( PermissionsAndroid . PERMISSIONS . WRITE_EXTERNAL_STORAGE ) ;
52+ }
53+ }
54+
55+ getMapZoom ( ) {
56+ const position = this . state . region ;
57+
58+ const bounds = [
59+ position . longitude - ( position . longitudeDelta / 2 ) ,
60+ position . latitude - ( position . latitudeDelta / 2 ) ,
61+ position . longitude + ( position . longitudeDelta / 2 ) ,
62+ position . latitude + ( position . latitudeDelta / 2 ) ,
63+ ] ;
64+
65+ return geoViewport . viewport ( bounds , [ width , height ] , 0 , 21 , 256 ) . zoom || 0 ;
66+ }
67+
68+ onRegionChangeComplete = ( region ) => {
69+ this . updateRegion ( region ) ;
70+ }
71+
72+ onRegionChange = ( region ) => {
73+ if ( this . onRegionChangeTimer ) {
74+ clearTimeout ( this . onRegionChangeTimer ) ;
75+ }
76+ this . onRegionChangeTimer = setTimeout ( ( ) => {
77+ this . updateRegion ( region ) ;
78+ } , 200 ) ;
79+ }
80+
81+ onMapPress = ( e ) => {
82+ const coordinates = e . nativeEvent . coordinate ;
83+ const zoom = this . getMapZoom ( ) ;
84+ const tile = tilebelt . pointToTile ( coordinates . longitude , coordinates . latitude , zoom , true ) ;
85+
86+ this . setState ( {
87+ coordinates : {
88+ tile : [ tile [ 0 ] , tile [ 1 ] , tile [ 2 ] ] ,
89+ precision : [ tile [ 3 ] , tile [ 4 ] ] ,
90+ } ,
91+ } ) ;
92+ }
93+
94+ updateRegion = ( region ) => {
95+ this . setState ( { region } ) ;
96+ }
97+
3998 render ( ) {
40- const { region } = this . state ;
99+ const { region, coordinates } = this . state ;
100+ const hasCoordinates = ( coordinates . tile && coordinates . tile . length === 3 ) || false ;
101+
41102 return (
42103 < View style = { styles . container } >
43104 < MapView
44105 provider = { this . props . provider }
45106 mapType = { this . mapType }
46107 style = { styles . map }
108+ mapType = "hybrid"
109+ onPress = { this . onMapPress }
47110 initialRegion = { region }
111+ onRegionChange = { this . onRegionChange }
112+ onRegionChangeComplete = { this . onRegionChangeComplete }
48113 >
49- < MapView . UrlTile
50- urlTemplate = "http://c.tile.stamen .com/watercolor/ {z}/{x}/{y}.jpg "
114+ < MapView . CanvasUrlTile
115+ urlTemplate = "http://wri-tiles.s3.amazonaws .com/glad_prod/tiles/ {z}/{x}/{y}.png "
51116 zIndex = { - 1 }
117+ maxZoom = { 12 }
118+ areaId = "Download"
119+ alertType = "umd_as_it_happen"
120+ isConnected
121+ minDate = "0"
122+ maxDate = "3000"
52123 />
124+ { hasCoordinates &&
125+ < MapView . CanvasInteractionUrlTile
126+ coordinates = { coordinates }
127+ urlTemplate = "http://wri-tiles.s3.amazonaws.com/glad_prod/tiles/{z}/{x}/{y}.png"
128+ zIndex = { - 1 }
129+ maxZoom = { 12 }
130+ areaId = "Download"
131+ alertType = "umd_as_it_happen"
132+ isConnected
133+ minDate = "0"
134+ maxDate = "3000"
135+ />
136+ }
53137 </ MapView >
54138 < View style = { styles . buttonContainer } >
55139 < View style = { styles . bubble } >
0 commit comments