簡體   English   中英

獲取前往Google Maps Marker的路線

[英]Get Directions to a Google Maps Marker

我正在嘗試從用戶可以在文本框中輸入的郵政編碼獲取指向Google Maps Marker位置的路線。 我正在使用Google Maps,Places and Directions Google圖書館。

我從Google找到了這個示例,並且正在嘗試使用它http://code.google.com/apis/maps/documentation/directions/

我想我在那里。 我希望用戶能夠從地圖上單擊標記,然后單擊顯示“來自標記一個的方向”的鏈接。 我可以使它正確顯示,並將正確的位置傳遞給函數,但是由於某種原因,它不能保留整個latlng坐標。 似乎它丟棄了其中一個,這就是為什么它引發錯誤。 如果我打印出傳遞給新函數(placeLoc)的內容,則只會顯示其中一個坐標。

它給出的錯誤是:未捕獲的錯誤:屬性的值無效:-0.5796900000000278

這是到目前為止我得到的代碼:

        function addPlaceResults(){

    var request = {
        location: midWayPoint,
        radius: 7000,
        types: [type]
    };

    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
    service.getDetails(request, callback);


    function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
            }
        }   
    }

    function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
            map: map,
            position: place.geometry.location
        });

        var request = { reference: place.reference };
        service.getDetails(request, function(details, status) {

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(
                    details.name + "<br />" 
                    + details.formatted_address + "<br />" 
                    + "<a target='_blank' href='" + details.website +"'>Visit Website</a>" + "<br />" 
                    + details.formatted_phone_number + "<br />"
                    + "<a href='#' onclick='dPoint1(" +placeLoc+ ")'>Directions from Marker One</a>" + "<br />"

                );
             infowindow.open(map, this);
            });
        });
    }
};

        //directions from point one
        function dPoint1(x) {
    console.log(x);
    directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer({
        suppressMarkers: true,
        suppressInfoWindows: true
    });

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));

    var request = {
        origin: address1, 
        destination: x,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status){
        if (status == google.maps.DirectionsStatus.OK) 
        {
            directionsDisplay.setDirections(response);
        }
    });
};

我試圖在此問題中僅包括相關代碼和詳細信息。 如果有任何我想念的東西或不清楚的地方,請告訴我,我將在/盡力說明。

再次感謝您的幫助。

干杯JA


編輯

好的,這很合理。 多謝你們。

我試過使用toString()方法,但這不起作用。 我可以使用toUrlValue()成功將帶有兩個值的字符串(以逗號分隔)傳遞到dPoint1函數中。 當我這樣做時,我可以使用console.log(x,y)將它們都打印到控制台,但實際上不能運行代碼。 我收到與以前相同的錯誤。

我認為問題是因為我需要將它們作為一件事,因此我可以將其添加到“目的地:”中,例如,它最終應該是目的地:x,但是因為我需要傳遞x&y,所以我必須要有目的地:x ,y,從而引發錯誤。

我猜我需要利用google.maps.LatLng()方法。

我在深夜添加了此內容:

newlatlng = new google.maps.LatLng((placeLoc.lat()+placeLoc.lat()));

這給了我一些指導,但沒有指引到正確的地方!

有人對我如何使用它有任何想法嗎?

非常感謝您到目前為止給我的所有幫助。

JA

這行不通

onclick='dPoint1(" +placeLoc+ ")'

因為您不能將對象傳遞給這樣的函數:將其串聯為字符串將產生一個字符串表示形式,該形式可能采用“ x,y”的形式。 您需要定義函數dPoint1以采用x和y(這可能會很好地起作用),但是最好顯式傳遞從placeLoc派生的文字值。

正如Andrew LeachAndrew Leach

onclick='dPoint1(" +placeLoc+ ")'

place.geometry.location用作place.geometry.locationgoogle.maps.LatLng對象: http : //code.google.com/apis/maps/documentation/javascript/reference.html#PlaceGeometry

但是google.maps.LatLng類具有toString()方法: http : //code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

您可以嘗試使用該方法,而不是將google.maps.LatLng對象傳遞給dPoint1()方法。

例如

onclick='dPoint1(" +placeLoc.toString()+ ")' 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM