twitter.com/keokilee | facebook.com/georgelee
github.com/keokilee
4/23/2015
react-native init AppWithAMap
cd AppWithAMap
open AppWithAMap.xcodeproj
var AppWithAMap = React.createClass({
render() {
return (
<NavigatorIOS style={styles.navigator}
initialRoute={{
component: Welcome,
title: "Welcome"
}}
/>
)
}
});
var Welcome = React.createClass({
...
});
var styles = StyleSheet.create({
navigator: {
flex: 1
},
...
});
var MapPoints = React.createClass({
getInitialState () {
var dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
return {
dataSource: dataSource.cloneWithRows(['foo', 'bar', 'baz'])
}
},
render: function() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(row) => <MapRow row={row} />} />
);
}
});
var MapRow = React.createClass({
render() {
return (
<View style={styles.row}>
<Text style={styles.text}>{this.props.row}</Text>
</View>
);
}
});
var MapRow = React.createClass({
render() {
var latitude = this.props.row.geometry.latitude;
var longitude = this.props.row.geometry.longitude;
var text = `Latitude: ${latitude}\nLongitude: ${longitude}`;
return (
<View style={styles.row}>
<Text style={styles.text}>
{text}
</Text>
</View>
);
}
});
var React = require('react-native');
var {
MapView,
StyleSheet
} = React;
var MapDisplay = React.createClass({
render() {
var {
latitude,
longitude,
title
} = this.props;
var annotation = [{latitude, longitude, title}];
var region = {latitude, longitude, latitudeDelta: 1, longitudeDelta: 1};
return (
<MapView style={styles.map} annotations={annotation} region={region} />
);
}
});
var styles = StyleSheet.create({
map: {
flex: 1
}
})
module.exports = MapDisplay;
var MapRow = React.createClass({
selected() {
var latitude = this.props.row.geometry.latitude;
var longitude = this.props.row.geometry.longitude;
this.props.navigator.push({
title: "Map",
component: MapDisplay,
passProps: {
latitude: latitude,
longitude: longitude,
title: "Point"
}
});
},
render() {
var latitude = this.props.row.geometry.latitude;
var longitude = this.props.row.geometry.longitude;
var text = `Latitude: ${latitude}\nLongitude: ${longitude}`;
return (
<TouchableHighlight onPress={this.selected}>
<View style={styles.row}>
<Text style={styles.text}>
{text}
</Text>
</View>
</TouchableHighlight>
);
}
});
var MapPoints = React.createClass({
componentDidMount() {
this.fetchData();
},
fetchData() {
fetch(REQUEST_URL)
.then((response => response.json()))
.then((responseData => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData)
});
}))
.done();
},
...
});