美文网首页
地图的实现

地图的实现

作者: 沐盺zz | 来源:发表于2018-03-26 16:00 被阅读0次

    地图的参考文档,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,如下

    /**

    */

    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 (

    <View style={styles.container}>

    <TabBarIOS>

    <TabBarIOS.Item

    title="卫生间"

    icon={require("./img/toilet.png")}

    selected={1==this.state.defaultIndex}

    onPress={this.ChangeTab.bind(this,1)}>

    <MyWebView uri="http://localhost:63342/rn/Tolit/Tolit/html/near.html?_ijt=ko2lhujoavcj9kmcupse62289a"></MyWebView>

    </TabBarIOS.Item>

    <TabBarIOS.Item

    title="电影"

    icon={require("./img/movie.png")}

    selected={2==this.state.defaultIndex}

    onPress={this.ChangeTab.bind(this,2)}

    <Text>我是电影</Text>

    </TabBarIOS.Item>

    <TabBarIOS.Item

    title="阅读"

    icon={require("./img/read.png")}

    selected={3==this.state.defaultIndex}

    onPress={this.ChangeTab.bind(this,3)}

    <Text>我是阅读</Text>

    </TabBarIOS.Item>

    <TabBarIOS.Item

    title="设置"

    icon={require("./img/set.png")}

    selected={4==this.state.defaultIndex}

    onPress={this.ChangeTab.bind(this,4)}

    <Text>我是设置</Text>

    </TabBarIOS.Item>

    </TabBarIOS>

    </View>

    );

    }

    }

    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 (<View style={styles.loadStyle}>

    <ActivityIndicator>

    <Text style={styles.wordStyle}>服务器错误或者网络地址错误!</Text>

    </ActivityIndicator>

    </View>)

    }else{

    return(<WebView

    source={{uri:this.props.uri}}

    onError={this.setError}

    </WebView>)

    }

    }

    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文件,代码如下

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">

    <title>卫生间</title>

    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.4&key=d003e159729b9244eaf57d98fa2e5be1"></script>

    <style type="text/css">

    body,html,#container{

    height: 100%;

    margin: 0px;

    }

    container .amap-controls .amap-scalecontrol{

    left: 50px;

    bottom: 30px;

    z-index:200;

    }

    .amap-geolocation-con{

    left: 10px;

    }

    </style>

    </head>

    <body>

    <div id="container" tabindex="0"></div>

    </body>

    <script type="text/javascript">

    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: <div style="background: orange;white-space: nowrap;padding: 0px 5px">${v.distance}米</div> //自定义点标记覆盖物内容

    });

    // 给marker绑定click事件

    AMap.event.addListener(marker, 'click', showInfoWin);

    function showInfoWin(){

    //显示信息窗口

    var infoWindow = new AMap.InfoWindow({

    isCustom: true, //使用自定义窗体

    content: `<div style="background: white;position: relative;">

    <p style="background: rgb(54,160,251);color: white;padding: 0px 10px">卫生间

    <span style="position: absolute;right: 10px" onclick="closeInfoWindow()">x</span>

    </p>

    <p style="padding: 0px 10px">名称:${v.name}</p>

    <p style="padding: 0px 10px">距您:${v.distance}米</p>

    <p style="padding: 0px 10px">地址:${v.address}</p>

    </div>`,

    offset: new AMap.Pixel(16, -45)

    });

    infoWindow.open(map,marker.getPosition());

    //根据起终点坐标规划步行路线

    walking.search(cpoint,v.location);

    }

    })

    }

    });

    });

    }

    }

    function onError(){

    console.log("定位失败");

    }

    //关闭窗口

    function closeInfoWindow() {

    map.clearInfoWindow();

    }

    </script>

    </html>

    ----key值需要去高德官网新建一个应用,选择web应用,就会得到相应的key值---

    image.png

    相关文章

      网友评论

          本文标题:地图的实现

          本文链接:https://www.haomeiwen.com/subject/syurcftx.html