简体   繁体   English

获取前往Google Maps Marker的路线

[英]Get Directions to a Google Maps Marker

I'm trying to get directions to a Google Maps Marker position from a postcode that a user can enter in a textbox. 我正在尝试从用户可以在文本框中输入的邮政编码获取指向Google Maps Marker位置的路线。 I'm using the Maps, Places and Directions Google Libraries. 我正在使用Google Maps,Places and Directions Google图书馆。

I've found this example from google and I'm trying to work from it http://code.google.com/apis/maps/documentation/directions/ 我从Google找到了这个示例,并且正在尝试使用它http://code.google.com/apis/maps/documentation/directions/

I think I'm pretty much there. 我想我在那里。 I want the user to be able to click on the marker from the map and then click a link that says 'Directions from Marker One'. 我希望用户能够从地图上单击标记,然后单击显示“来自标记一个的方向”的链接。 I can get this to appear correctly and I pass the correct location into a function but for some reason it doesn't keep the entire latlng coordinates. 我可以使它正确显示,并将正确的位置传递给函数,但是由于某种原因,它不能保留整个latlng坐标。 It looks like it drops one of them and that's why it throws an error. 似乎它丢弃了其中一个,这就是为什么它引发错误。 If i print out what I pass to the new function (placeLoc) it's only showing one of the coordinates. 如果我打印出传递给新函数(placeLoc)的内容,则只会显示其中一个坐标。

the error it gives is: Uncaught Error: Invalid value for property : -0.5796900000000278 它给出的错误是:未捕获的错误:属性的值无效:-0.5796900000000278

heres the code I've got so far: 这是到目前为止我得到的代码:

        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);
        }
    });
};

I've tried to include only the relevant code and details in this question. 我试图在此问题中仅包括相关代码和详细信息。 If there is anything I've missed or anything that isn't clear please let me know and I'll add it in / do my best to clarify myself. 如果有任何我想念的东西或不清楚的地方,请告诉我,我将在/尽力说明。

Once again, I'll greatly appreciate any help. 再次感谢您的帮助。

Cheers JA 干杯JA


Edit 编辑

Ok, that makes perfect sense. 好的,这很合理。 Thanks guys. 多谢你们。

I've tried using the toString() method but that doesn't work. 我试过使用toString()方法,但这不起作用。 I can successfully pass a string with the two values, separated by a comma into dPoint1 function by using toUrlValue(). 我可以使用toUrlValue()成功将带有两个值的字符串(以逗号分隔)传递到dPoint1函数中。 When I do this I can print them both out to the console using console.log(x,y) but I can't actually run the code. 当我这样做时,我可以使用console.log(x,y)将它们都打印到控制台,但实际上不能运行代码。 I get the same error as before. 我收到与以前相同的错误。

I think the problem is because I need them to be one thing so i can add it to 'destination: ' for example it should ultimately be destination: x, but because I need to pass x & y in I have to have destination: x ,y, which throws the error. 我认为问题是因为我需要将它们作为一件事,因此我可以将其添加到“目的地:”中,例如,它最终应该是目的地:x,但是因为我需要传递x&y,所以我必须要有目的地:x ,y,从而引发错误。

I'm guessing I need to make use of the google.maps.LatLng() method. 我猜我需要利用google.maps.LatLng()方法。

I added this in late last night: 我在深夜添加了此内容:

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

that gave me some directions but not to the right place! 这给了我一些指导,但没有指引到正确的地方!

Does anyone have any ideas on how I could use this? 有人对我如何使用它有任何想法吗?

Thanks so much for all the help you've given me so far. 非常感谢您到目前为止给我的所有帮助。

JA JA

This won't work 这行不通

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

because you can't pass objects to functions like that: concatenating it as a string will produce a string representation which will probably be in the form "x,y". 因为您不能将对象传递给这样的函数:将其串联为字符串将产生一个字符串表示形式,该形式可能采用“ x,y”的形式。 You need to define function dPoint1 to take x and y -- which may well work -- but it would be better to pass literal values explicitly which you derive from placeLoc. 您需要定义函数dPoint1以采用x和y(这可能会很好地起作用),但是最好显式传递从placeLoc派生的文字值。

As mentioned by Andrew Leach : 正如Andrew LeachAndrew Leach

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

Wont work as place.geometry.location is a google.maps.LatLng object: http://code.google.com/apis/maps/documentation/javascript/reference.html#PlaceGeometry place.geometry.location用作place.geometry.locationgoogle.maps.LatLng对象: http : //code.google.com/apis/maps/documentation/javascript/reference.html#PlaceGeometry

However the google.maps.LatLng class has a toString() method: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng 但是google.maps.LatLng类具有toString()方法: http : //code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

You could try using that instead of passing the google.maps.LatLng object to the dPoint1() method. 您可以尝试使用该方法,而不是将google.maps.LatLng对象传递给dPoint1()方法。

Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM