地图的参考文档,http://lbs.amap.com/api/javascript-api/gettingstarted
1.在webstrom中创建一个新的项目:
$ react-native init project --version 0.44.3
(project---项目名)
(version---版本号)
2.项目创建成功以后,双击ios文件夹中的后缀为.xcodeproj的文件,在xcode上面运行该项目
3.在文件夹中的index.ios.js书写TabBar,如下
/**
Sample React Native App
https://github.com/facebook/react-native
@flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TabBarIOS
} from 'react-native';
import MyWebView from './common/MyWebView'
export default class Tolit extends Component {
constructor(props){
super(props);
this.state = {
defaultIndex:1
}
}
ChangeTab(index){
// alert(index);
this.setState({defaultIndex:index});
}
render() {
return (
title="卫生间" icon={require("./img/toilet.png")} selected={1==this.state.defaultIndex} onPress={this.ChangeTab.bind(this,1)}>
title="电影" icon={require("./img/movie.png")} selected={2==this.state.defaultIndex} onPress={this.ChangeTab.bind(this,2)}
title="阅读" icon={require("./img/read.png")} selected={3==this.state.defaultIndex} onPress={this.ChangeTab.bind(this,3)}
title="设置" icon={require("./img/set.png")} selected={4==this.state.defaultIndex} onPress={this.ChangeTab.bind(this,4)}
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
AppRegistry.registerComponent('Tolit', () => Tolit);
4.MyWebView是一个组件,为了使错误变得通俗易懂,如下
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
WebView,
ActivityIndicator
} from 'react-native';
class MyWebView extends Component{
constructor(props){
super(props);
this.setError = this.setError.bind(this);
this.state={
isError:false
}
}
render(){
if(this.state.isError){
return (
)
}else{
return( source={{uri:this.props.uri}} onError={this.setError}
}
}
setError(){
this.setState({isError:true})
}
}
const styles = StyleSheet.create({
loadStyle:{
flex:1,
// justifyContent:"center",
color:"red"
},
wordStyle:{
color:"red"
}
})
export default MyWebView;
5.新建一个html文件夹,在该文件夹下建一个html文件,代码如下
body,html,#container{
height: 100%;
margin: 0px;
}
container .amap-controls .amap-scalecontrol{
left: 50px;
bottom: 30px;
z-index:200;
}
.amap-geolocation-con{
left: 10px;
}
var map = new AMap.Map('container',{
resizeEnable: true,
zoom: 15
});
AMap.plugin(['AMap.ToolBar','AMap.Scale','AMap.OverView'],
function(){
map.addControl(new AMap.ToolBar());
map.addControl(new AMap.Scale());
});
//实例化walking
AMap.service(["AMap.Walking"], function(){
walking = new AMap.Walking({
map: map,
});
})
//在指定范围内搜寻所要找的地方
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
buttonPosition:'RB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();//自动定位
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
function onComplete(data){
console.log("定位成功的data值:",data);
if(data.info == 'SUCCESS' && data.status == 1){
//定位成功;--》根据定位的坐标查找附近的卫生间
AMap.service(["AMap.PlaceSearch"], function() {
var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
pageSize: 5, //个数
pageIndex: 1,
map: map
});
var cpoint = data.position; //中心点坐标
placeSearch.searchNearBy('卫生间', cpoint, 2000, function(status, result) {
console.log(status,result);
if(status == 'complete'){
var toilets = result.poiList.pois;
toilets.forEach(function(v,k) {
console.log("--", v);
var marker = new AMap.Marker({ //添加自定义点标记
map: map,
position: v.location, //基点位置
offset: new AMap.Pixel(-20, -55), //相对于基点的偏移位置
draggable: false, //是否可拖动
content:
//自定义点标记覆盖物内容
});
// 给marker绑定click事件
AMap.event.addListener(marker, 'click', showInfoWin);
function showInfoWin(){
//显示信息窗口
var infoWindow = new AMap.InfoWindow({
isCustom: true, //使用自定义窗体
content: `
卫生间
x
名称:${v.name}
距您:${v.distance}米
地址:${v.address}
offset: new AMap.Pixel(16, -45)
});
infoWindow.open(map,marker.getPosition());
//根据起终点坐标规划步行路线
walking.search(cpoint,v.location);
}
})
}
});
});
}
}
function onError(){
console.log("定位失败");
}
//关闭窗口
function closeInfoWindow() {
map.clearInfoWindow();
}
----key值需要去高德官网新建一个应用,选择web应用,就会得到相应的key值---