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