目的地点击后,创建从原点到目标地图标记的路线(行驶)

时间:2018-02-20 15:34:50

标签: javascript asp.net-mvc google-maps google-maps-api-3 google-maps-markers

我尝试在点击目标标记后在源标记(蓝色标记)和目标之间创建routeLine(驱动) enter image description here

这是我的javascripts

function initMap() {
    //read the parameter values you want to send to server
    var searchItem = $("#SearchItem").val();
    var jobs = $("#Jobs").val();
    var subid = $("#bluee").val();

    var map = new google.maps.Map(document.getElementById('dvMap'),
        {
            zoom: 8
        });

 var url = "@Url.Action("AsMapAjax", "My")";


    navigator.geolocation.getCurrentPosition(function (p) {
        var latlngg = new google.maps.LatLng(p.coords.latitude, 
          p.coords.longitude);

        var mapOptions = {
            center: latlngg,
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        //You are Here

        var iconoMarca = "../../images/bnm.png";
        mymarker = new google.maps.Marker({
            animation: google.maps.Animation.DROP,
            map: map,
            icon: iconoMarca,
            position: latlngg,
            optimized: false,
            title:"شما اینجا هستید"

        });

        var numberMarkerImg = {
            url: '../../images/webresize2.png',

            labelOrigin: new google.maps.Point(30, -6)
        };


        $.post(url, { SearchItem: searchItem, jobid: jobs, subid: subid }, 
    function (res) {
            if (res.success) {
                var latLng;
                $.each(res.responseText, function (i, item) {
                    latLng = new google.maps.LatLng(item.Lat, item.Lng);
                    var marker = new google.maps.Marker({
                        position: latLng,
                        map: map,
                        icon: numberMarkerImg,

                        label: {
                            text: item.Title,
                            color: 'Black',
                            fontFamily: 'IranSans',
                            fontSize: '17'
                        },

                        animation: google.maps.Animation.DROP
                    });

                   var wdLink = '@Url.Action("Poster", "My")?id=' + item.Title.split(" ").join("-") + '__' + item.Id;

                    var contentString = '<div id="content">' +
'<div id="siteNotice">' +
'</div>' +
'<div  id="firstHeading">' + item.Title + '</div>' +
'<div id="bodyContent">' +
'<p><b>' + item.Preamble + '</b></p>' +
'<p><h4> <a href="'+ wdLink+'">' +
'اطلاعات بیشتر</a></h4> ' +
'</p>' +
'</div>' +
'</div>';

               var infowindow = new google.maps.InfoWindow({
                        content: contentString
                    });
                    marker.addListener('click', function () {
                        infowindow.open(map, marker);

                    });

                     (marker, item);
                    ///////////////////
                    $(document).ready(function () {
                        $(window).resize(function () {
                            google.maps.event.trigger(map, 'resize');
                        });
                        google.maps.event.trigger(map, 'resize');
                    });


                });
                map.setCenter(latlngg);
            }
        });
    });
}

不幸的是,经过多方努力后我无法解决这个问题。你能救我吗? 我做了一次,但是使用Polyline,我现在正在寻找一条从原点到目的地的街道路径,点击目的地

1 个答案:

答案 0 :(得分:1)

查看this JSBin,了解如何实现您正在寻找的功能的简单示例。

以下函数获取路径并进行渲染:

function markerClicked(destinationLocation) {
  var directionsRequest = {
    origin: sourceLocation,
    destination: destinationLocation,
    travelMode: 'DRIVING'
  };

  directionsService.route(directionsRequest, handleDirectionResults);
}

function handleDirectionResults(result, status) {
  if (status === 'OK') {
    directionsDisplay.setDirections(result);
  } else {
    console.log(status);
  }
}

单击标记时,将调用markerClicked函数,并将其作为参数传递到标记的位置。 markerClicked发出路线搜索请求,然后使用请求结果调用handleDirectionResultshandleDirectionResults通过调用setDirections对象上的directionsDisplay方法呈现路线(将路线请求的结果传递给setDirections)。

相关问题