File size: 10,196 Bytes
b3787d8 | 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | 'use strict';
var React = require('react-native');
var {
EdgeInsetsPropType,
NativeMethodsMixin,
Platform,
ReactNativeViewAttributes,
View,
Animated,
requireNativeComponent,
NativeModules,
PropTypes,
} = React;
var MapMarker = require('./MapMarker');
var MapPolyline = require('./MapPolyline');
var MapPolygon = require('./MapPolygon');
var MapCircle = require('./MapCircle');
var MapCallout = require('./MapCallout');
var MapView = React.createClass({
mixins: [NativeMethodsMixin],
viewConfig: {
uiViewClassName: 'AIRMap',
validAttributes: {
region: true,
},
},
propTypes: {
...View.propTypes,
/**
* Used to style and layout the `MapView`. See `StyleSheet.js` and
* `ViewStylePropTypes.js` for more info.
*/
style: View.propTypes.style,
/**
* If `true` the app will ask for the user's location and focus on it.
* Default value is `false`.
*
* **NOTE**: You need to add NSLocationWhenInUseUsageDescription key in
* Info.plist to enable geolocation, otherwise it is going
* to *fail silently*!
*/
showsUserLocation: PropTypes.bool,
/**
* If `false` points of interest won't be displayed on the map.
* Default value is `true`.
*
*/
showsPointsOfInterest: PropTypes.bool,
/**
* If `false` compass won't be displayed on the map.
* Default value is `true`.
*
* @platform ios
*/
showsCompass: PropTypes.bool,
/**
* If `false` the user won't be able to pinch/zoom the map.
* Default value is `true`.
*
*/
zoomEnabled: PropTypes.bool,
/**
* If `false` the user won't be able to pinch/rotate the map.
* Default value is `true`.
*
*/
rotateEnabled: PropTypes.bool,
/**
* If `false` the user won't be able to change the map region being displayed.
* Default value is `true`.
*
*/
scrollEnabled: PropTypes.bool,
/**
* A Boolean indicating whether the map shows scale information.
* Default value is `false`
*
*/
showsScale: PropTypes.bool,
/**
* A Boolean indicating whether the map displays extruded building information.
* Default value is `true`.
*/
showsBuildings: PropTypes.bool,
/**
* A Boolean value indicating whether the map displays traffic information.
* Default value is `false`.
*/
showsTraffic: PropTypes.bool,
/**
* A Boolean indicating whether indoor maps should be enabled.
* Default value is `false`
*
* @platform android
*/
showsIndoors: PropTypes.bool,
/**
* The map type to be displayed.
*
* - standard: standard road map (default)
* - satellite: satellite view
* - hybrid: satellite view with roads and points of interest overlayed
*/
mapType: PropTypes.oneOf([
'standard',
'satellite',
'hybrid',
]),
/**
* The region to be displayed by the map.
*
* The region is defined by the center coordinates and the span of
* coordinates to display.
*/
region: PropTypes.shape({
/**
* Coordinates for the center of the map.
*/
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
/**
* Difference between the minimun and the maximum latitude/longitude
* to be displayed.
*/
latitudeDelta: PropTypes.number.isRequired,
longitudeDelta: PropTypes.number.isRequired,
}),
/**
* The initial region to be displayed by the map. Use this prop instead of `region`
* only if you don't want to control the viewport of the map besides the initial region.
*
* Changing this prop after the component has mounted will not result in a region change.
*
* This is similar to the `initialValue` prop of a text input.
*/
initialRegion: PropTypes.shape({
/**
* Coordinates for the center of the map.
*/
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
/**
* Difference between the minimun and the maximum latitude/longitude
* to be displayed.
*/
latitudeDelta: PropTypes.number.isRequired,
longitudeDelta: PropTypes.number.isRequired,
}),
/**
* Maximum size of area that can be displayed.
*
* @platform ios
*/
maxDelta: PropTypes.number,
/**
* Minimum size of area that can be displayed.
*
* @platform ios
*/
minDelta: PropTypes.number,
/**
* Insets for the map's legal label, originally at bottom left of the map.
* See `EdgeInsetsPropType.js` for more information.
*/
legalLabelInsets: EdgeInsetsPropType,
/**
* Callback that is called continuously when the user is dragging the map.
*/
onRegionChange: PropTypes.func,
/**
* Callback that is called once, when the user is done moving the map.
*/
onRegionChangeComplete: PropTypes.func,
/**
* Callback that is called when user taps on the map.
*/
onPress: PropTypes.func,
/**
* Callback that is called when user makes a "long press" somewhere on the map.
*/
onLongPress: PropTypes.func,
/**
* Callback that is called when a marker on the map is tapped by the user.
*/
onMarkerPress: PropTypes.func,
/**
* Callback that is called when a marker on the map becomes selected. This will be called when
* the callout for that marker is about to be shown.
*
* @platform ios
*/
onMarkerSelect: PropTypes.func,
/**
* Callback that is called when a marker on the map becomes deselected. This will be called when
* the callout for that marker is about to be hidden.
*
* @platform ios
*/
onMarkerDeselect: PropTypes.func,
/**
* Callback that is called when a callout is tapped by the user.
*/
onCalloutPress: PropTypes.func,
/**
* Callback that is called when the user initiates a drag on a marker (if it is draggable)
*/
onMarkerDragStart: PropTypes.func,
/**
* Callback called continuously as a marker is dragged
*/
onMarkerDrag: PropTypes.func,
/**
* Callback that is called when a drag on a marker finishes. This is usually the point you
* will want to setState on the marker's coordinate again
*/
onMarkerDragEnd: PropTypes.func,
},
getInitialState: function() {
return {
isReady: Platform.OS === 'ios',
};
},
componentDidMount: function() {
const { region, initialRegion } = this.props;
if (region && this.state.isReady) {
this.refs.map.setNativeProps({ region });
} else if (initialRegion && this.state.isReady) {
this.refs.map.setNativeProps({ region: initialRegion });
}
},
componentWillUpdate: function(nextProps) {
var a = this.__lastRegion;
var b = nextProps.region;
if (!a || !b) return;
if (
a.latitude !== b.latitude ||
a.longitude !== b.longitude ||
a.latitudeDelta !== b.latitudeDelta ||
a.longitudeDelta !== b.longitudeDelta
) {
this.refs.map.setNativeProps({ region: b });
}
},
_onMapReady: function() {
const { region, initialRegion } = this.props;
if (region) {
this.refs.map.setNativeProps({ region });
} else if (initialRegion) {
this.refs.map.setNativeProps({ region: initialRegion });
}
this.setState({ isReady: true });
},
_onLayout: function(e) {
const { region, initialRegion, onLayout } = this.props;
const { isReady } = this.state;
if (region && isReady && !this.__layoutCalled) {
this.__layoutCalled = true;
this.refs.map.setNativeProps({ region });
} else if (initialRegion && isReady && !this.__layoutCalled) {
this.__layoutCalled = true;
this.refs.map.setNativeProps({ region: initialRegion });
}
onLayout && onLayout(e);
},
_onChange: function(event: Event) {
this.__lastRegion = event.nativeEvent.region;
if (event.nativeEvent.continuous) {
this.props.onRegionChange &&
this.props.onRegionChange(event.nativeEvent.region);
} else {
this.props.onRegionChangeComplete &&
this.props.onRegionChangeComplete(event.nativeEvent.region);
}
},
animateToRegion: function (region, duration) {
this._runCommand('animateToRegion', [region, duration || 500]);
},
animateToCoordinate: function (latLng, duration) {
this._runCommand('animateToCoordinate', [latLng, duration || 500]);
},
fitToElements: function(animated) {
this._runCommand('fitToElements', [animated]);
},
_getHandle: function() {
return React.findNodeHandle(this.refs.map);
},
_runCommand: function (name, args) {
switch (Platform.OS) {
case 'android':
NativeModules.UIManager.dispatchViewManagerCommand(
this._getHandle(),
NativeModules.UIManager.AIRMap.Commands[name],
args
);
break;
case 'ios':
NativeModules.AIRMapManager[name].apply(
NativeModules.AIRMapManager[name],
[this._getHandle(), ...args]
);
break;
}
},
render: function() {
let props;
if (this.state.isReady) {
props = {
...this.props,
region: null,
initialRegion: null,
onChange: this._onChange,
onMapReady: this._onMapReady,
onLayout: this._onLayout,
};
} else {
props = {
region: null,
initialRegion: null,
onChange: this._onChange,
onMapReady: this._onMapReady,
onLayout: this._onLayout,
};
}
return (
<AIRMap ref="map" {...props} />
);
},
});
var AIRMap = requireNativeComponent('AIRMap', MapView, {
nativeOnly: {
onChange: true,
onMapReady: true,
},
});
MapView.Marker = MapMarker;
MapView.Polyline = MapPolyline;
MapView.Polygon = MapPolygon;
MapView.Circle = MapCircle;
MapView.Callout = MapCallout;
MapView.Animated = Animated.createAnimatedComponent(MapView);
module.exports = MapView;
|