0

我试图从用户可以在文本框中输入的邮政编码获取Google地图标记位置的路线。我正在使用地图,地点和路线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

+0

使用逗号产生两个参数的经纬度(这需要两个数字,我很惊讶,它只有一个工作!):'newlatlng =新google.maps.LatLng(placeLoc.lat(),placeLoc ());' – 2012-03-06 14:56:46

+0

不确定是否有拼写错误,但尝试将第二个值作为placeLoc.lng() 'newlatlng = new google.maps.LatLng(placeLoc.lat(),placeLoc.lng()); ' – 2012-03-06 22:22:21

+0

感谢您的帮助。我终于在昨晚整理了它。新代码看起来更像这样:'+“Directions from Marker One”'然后我修改dPoint1接受x&y,然后我在dPoint1函数顶部创建了'var newlatlng = new google.maps.LatLng(x,y);'并以newlatlng为原点。感谢您与我一起坚持! JA – Jim 2012-03-07 10:29:46

回答

1

这将无法正常工作

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

,因为你不能传递对象,这样的功能:它串联为一个字符串会产生一个字符串表示这可能会在形式“X,Y”。您需要定义函数dPoint1以获取x和y - 这可能很有用 - 但最好从placeLoc中直接传递显式的文字值。

0

如前所述通过Andrew Leach

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

不会工作作为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()方法。

E.g.

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

这就是我所说的。 'onclick ='dPoint1(“+ placeLoc +”)''使用'.toString()'方法,现有的dPoint1()函数只接受一个参数。改变函数接受两个参数可能会奏效;但它不是很容易维护,因为'placeLoc'变成两个以逗号分隔的数字并不明显。这应该明确地做,以便将来变得更容易(以及针对'.toString()'格式更改的未来证明)。 – 2012-03-06 14:42:16