2012-03-02 61 views
0

此代碼是爲我編寫的,用於工作,但現在多個航點不工作。 航點存儲爲| 51.105166,-1.695971 | 51.105166,-1.695971 |在數據庫中。 只有1個航點的地圖工作,但不止一個,他們根本沒有出現。單個航點工作,多個航點不,任何想法?

有沒有人有任何建議?

它可以在這裏的鏈接查看。 http://www.ridersguide.co.uk/2012/Ride_234

var directionDisplay; 
var geocoder; 
var directionsService = new google.maps.DirectionsService(); 
var latlng = new google.maps.LatLng(54.559322587438636, -4.1748046875); 
function load() { 
    geocoder = new google.maps.Geocoder(); 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var myOptions = { 
    zoom: 6, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
center: latlng, 
    mapTypeControl: true, 
    mapTypeControlOptions: { 
    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
    position: google.maps.ControlPosition.TOP_RIGHT 
    },  

navigationControl: true, 
navigationControlOptions: { 
style: google.maps.NavigationControlStyle.ZOOM_PAN, 
position: google.maps.ControlPosition.TOP_LEFT} 
    }; 
    var map = new google.maps.Map(document.getElementById('map'), myOptions); 
    directionsDisplay.setMap(map); 
    directionsDisplay.setPanel(document.getElementById("directionsPanel")); 

var directionRendererOptions ={ 
    suppressMarkers: true, 
    polylineOptions:{ 
     strokeColor: "#FF0000", 
     strokeOpacity: 1, 
     strokeWeight: 3 
     } 
    }; 

directionsDisplay.setOptions(directionRendererOptions); 


      var start = '<?php echo $start; ?>'; 
      var end = '<?php echo $end; ?>'; 
      <?php 
      if($via != null){ 
      echo "var points = ["; 
      foreach($via as $point){ 
      if($point != ""){ 
      echo "{location:"; 
      echo " '".$point."'"; 
      echo "}"; 
      } 
      } 
      echo "];\n"; 
      } 
      ?> 

      var request = { 
       origin:start, 
       waypoints: points, 
       destination:end, 
       travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }; 
      directionsService.route(request, function(response, status) { 
       if (status == google.maps.DirectionsStatus.OK) { 
        directionsDisplay.setDirections(response); 

       } 

      }); 

     geocoder.geocode({ 'address': start}, function(results, status) { 
     var routeStart = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location, 
     icon: './images/motorcycling.png', 
     shadow: './images/motorcycling.shadow.png' 
    }); 

}); 
    geocoder.geocode({ 'address': end}, function(results, status) { 
     var routeEnd = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location, 
     icon: './images/motorcyclingend.png', 
     shadow: './images/motorcycling.shadow.png' 
    }); 

}); 
<?php` 

回答

0

這似乎有一個逗號陣列中丟失。

var points = [{location: '51.105166,-1.695971'}{location: '51.105166,-1.695971'}]; 

應該做的

var points = [{location: '51.105166,-1.695971'},{location: '51.105166,-1.695971'}]; 

一種方式,是完全構建字符串在PHP中,而不是將它輸出零零碎碎,然後刪除不需要的初始逗號。我已經編輯這個代碼以下收到的意見,所以只在被構造[]和逗號除去之間的位置:

<?php 
      if($via != null){ 
      $pointstring=''; 
      foreach($via as $point){ 
      if($point != ""){ 
      $pointstring.= ",{location:"; 
      $pointstring.= " '".$point."'"; 
      $pointstring.= "}"; 
      } 
      } 
      echo "var points = [".substr($pointstring,1)."];\n"; 
      } 
      ?> 

(代碼未測試)

有輸出該位置的其它方式就像你曾經有過的那樣,但是要求在它們之間有逗號,而不是在最後,這會比簡單地移除一個你不需要的更復雜。

+0

謝謝安德魯,我只需要找到擺脫最後一個逗號現在的方式。 – Endorphin34 2012-03-03 14:40:55

+0

我的方式不會添加最後一個逗號:它會在任何之前添加第一個逗號,然後將其刪除。最後一個逗號在哪裏?無論如何,刪除最後一個字符是'substr($ s,0,strlen($ s)-1)' – 2012-03-03 14:45:16

+0

嗨,安德魯,你的權利,對不起,我正在看舊的代碼。 – Endorphin34 2012-03-03 16:29:09