Google Map如么课程 04 做导航

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script language="javascript" type="text/javascript">
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();

    function InitializeMap() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions =
        {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"), myOptions);

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('directionpanel'));

        var control = document.getElementById('control');
        control.style.display = 'block'; 


    }



    function calcRoute() {
        var start = document.getElementById('startvalue').value;
        var end = document.getElementById('endvalue').value;
        
        start = new google.maps.LatLng(39.9,116.3);
        end  = new google.maps.LatLng(31.0,121.1);
        
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

    }



    function Button1_onclick() {
        calcRoute();
    }

    window.onload = InitializeMap;
    </script>
    
<table id ="control">
<tr>
<td>
<table>
<tr>
<td>From:</td>
<td>
    <input id="startvalue" type="text" style="width: 305px" /></td>
</tr>
<tr>
<td>To:</td>
<td><input id="endvalue" type="text" style="width: 301px" /></td>
</tr>
<tr>
<td align ="right">
    <input id="Button1" type="button" value="GetDirections" οnclick="return Button1_onclick()" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign ="top">
<div id ="directionpanel"  style="height: 390px;overflow: auto" ></div>
</td>
<td valign ="top">
<div id ="map" style="height: 390px; width: 489px"></div>
</td>
</tr>
</table>
</asp:Content>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中的地图导航功能可以通过调用小程序的API来实现。具体的步骤如下: 1. 在小程序的页面中引入地图组件,使用`map`标签在页面上展示地图。 2. 在小程序的`app.json`文件中配置地图组件的权限,需要在`permission`字段中添加`"scope.userLocation"`权限,以便获取用户的地理位置信息。 3. 在页面的js文件中编写代码,以获取用户地理位置为例,通过`wx.getLocation`方法获取用户当前位置的经纬度信息。 4. 调用微信地图API,使用获取到的经纬度信息显示地图,并根据用户选择的目的地,在地图上添加导航标记。 5. 用户在小程序中选择目的地后,通过`wx.openLocation`方法打开手机系统地图,实现导航功能。 例如,可以在小程序的`index.wxml`文件中添加地图组件的引用: ```html <!-- index.wxml --> <view> <map id="myMap" class="map"></map> </view> ``` 在`index.js`文件中编写获取用户位置的代码: ```javascript // index.js Page({ data: { latitude: 0, longitude: 0, }, onLoad() { wx.getLocation({ type: 'gcj02', success: (res) => { this.setData({ latitude: res.latitude, longitude: res.longitude, }) }, fail: (error) => { console.error('获取位置失败', error) }, }) }, }) ``` 最后,在用户点击选择目的地后,可以使用`wx.openLocation`方法打开手机系统地图进行导航: ```javascript // index.js Page({ // ... navigateToDestination() { wx.openLocation({ latitude: destinationLatitude, longitude: destinationLongitude, name: '目的地名称', address: '目的地地址' }); }, }) ``` 通过以上步骤,便可以在微信小程序中实现地图导航功能。当用户点击选择目的地后,系统便会打开手机系统地图,并提供导航路线信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值