/** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * The examples provided by Facebook are for non-commercial testing and * evaluation purposes only. * * Facebook reserves all rights not expressly granted. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @flow */ 'use strict'; var React = require('react'); var ReactNative = require('react-native'); var { PropTypes } = React; var { Image, MapView, StyleSheet, Text, TextInput, TouchableOpacity, View, } = ReactNative; var regionText = { latitude: '0', longitude: '0', latitudeDelta: '0', longitudeDelta: '0', }; var MapRegionInput = React.createClass({ propTypes: { region: PropTypes.shape({ latitude: PropTypes.number.isRequired, longitude: PropTypes.number.isRequired, latitudeDelta: PropTypes.number, longitudeDelta: PropTypes.number, }), onChange: PropTypes.func.isRequired, }, getInitialState() { return { region: { latitude: 0, longitude: 0, } }; }, componentWillReceiveProps: function(nextProps) { this.setState({ region: nextProps.region || this.getInitialState().region }); }, render: function() { var region = this.state.region || this.getInitialState().region; return ( {'Latitude'} {'Longitude'} {'Latitude delta'} {'Longitude delta'} {'Change'} ); }, _onChangeLatitude: function(e) { regionText.latitude = e.nativeEvent.text; }, _onChangeLongitude: function(e) { regionText.longitude = e.nativeEvent.text; }, _onChangeLatitudeDelta: function(e) { regionText.latitudeDelta = e.nativeEvent.text; }, _onChangeLongitudeDelta: function(e) { regionText.longitudeDelta = e.nativeEvent.text; }, _change: function() { this.setState({ region: { latitude: parseFloat(regionText.latitude), longitude: parseFloat(regionText.longitude), latitudeDelta: parseFloat(regionText.latitudeDelta), longitudeDelta: parseFloat(regionText.longitudeDelta), }, }); this.props.onChange(this.state.region); }, }); class MapViewExample extends React.Component { state = { isFirstLoad: true, mapRegion: undefined, mapRegionInput: undefined, annotations: [], }; render() { return ( ); } _getAnnotations = (region) => { return [{ longitude: region.longitude, latitude: region.latitude, title: 'You Are Here', }]; }; _onRegionChange = (region) => { this.setState({ mapRegionInput: region, }); }; _onRegionChangeComplete = (region) => { if (this.state.isFirstLoad) { this.setState({ mapRegionInput: region, annotations: this._getAnnotations(region), isFirstLoad: false, }); } }; _onRegionInputChanged = (region) => { this.setState({ mapRegion: region, mapRegionInput: region, annotations: this._getAnnotations(region), }); }; } class AnnotationExample extends React.Component { state = { isFirstLoad: true, annotations: [], mapRegion: undefined, }; render() { if (this.state.isFirstLoad) { var onRegionChangeComplete = (region) => { this.setState({ isFirstLoad: false, annotations: [{ longitude: region.longitude, latitude: region.latitude, ...this.props.annotation, }], }); }; } return ( ); } } class DraggableAnnotationExample extends React.Component { state = { isFirstLoad: true, annotations: [], mapRegion: undefined, }; createAnnotation = (longitude, latitude) => { return { longitude, latitude, draggable: true, onDragStateChange: (event) => { if (event.state === 'idle') { this.setState({ annotations: [this.createAnnotation(event.longitude, event.latitude)], }); } console.log('Drag state: ' + event.state); }, }; }; render() { if (this.state.isFirstLoad) { var onRegionChangeComplete = (region) => { //When the MapView loads for the first time, we can create the annotation at the //region that was loaded. this.setState({ isFirstLoad: false, annotations: [this.createAnnotation(region.longitude, region.latitude)], }); }; } return ( ); } } var styles = StyleSheet.create({ map: { height: 150, margin: 10, borderWidth: 1, borderColor: '#000000', }, row: { flexDirection: 'row', justifyContent: 'space-between', }, textInput: { width: 150, height: 20, borderWidth: 0.5, borderColor: '#aaaaaa', fontSize: 13, padding: 4, }, changeButton: { alignSelf: 'center', marginTop: 5, padding: 3, borderWidth: 0.5, borderColor: '#777777', }, }); exports.displayName = (undefined: ?string); exports.title = ''; exports.description = 'Base component to display maps'; exports.examples = [ { title: 'Map', render() { return ; } }, { title: 'showsUserLocation + followUserLocation', render() { return ( ); } }, { title: 'Callout example', render() { return { alert('You Are Here'); }}> ), }}/>; } }, { title: 'Annotation focus example', render() { return { alert('Annotation gets focus'); }, onBlur: () => { alert('Annotation lost focus'); } }}/>; } }, { title: 'Draggable pin', render() { return ; } }, { title: 'Custom pin color', render() { return ; } }, { title: 'Custom pin image', render() { return ; } }, { title: 'Custom pin view', render() { return Thumbs Up! , }}/>; } }, { title: 'Custom overlay', render() { return ; } }, ];