2012-02-03 142 views
1

我正在物業租賃網站上工作。在該網站上,我希望有一張Google地圖,標有所有屬性,並繪製本地巴士路線,以便租客可以看到屬性與路線的距離。使用谷歌地圖繪製巴士路線

我已經完成了問題的第一部分;我已經使用標記繪製了屬性。現在我需要添加公交路線。

我已經看了這個,我不能完全找到實現它的最佳方式。我查看了多段線,並使用this tool,但路線很複雜,需要數百個座標。

有一些種類的路由API,如this post,但顯然它只能採用8個waypoints。是對的嗎?

理想情況下,我想通過選擇一個起點,一個終點並拖拽路線來繪製地圖;然後以某種方式將該路線導入我擁有的路線。

以下是我想要導入的確切路線:http://www2.warwick.ac.uk/services/accommodation/landlords/12busroutes/

我的代碼繪製的屬性是:

var siteRoot = "<?php bloginfo('stylesheet_directory');?>/"; 

var markers = [ 
    <?php 
    $my_query = new WP_Query('post_type=properties'); 
    while ($my_query->have_posts()) : $my_query->the_post(); 
     kdev_maps('list'); 
    endwhile; // end of the loop. 
    ?> 
]; 

function googlemap() { 

jQuery('#map_canvas').css({'height': '400px'}); 

// Create the map 
// No need to specify zoom and center as we fit the map further down. 
var map = new google.maps.Map(document.getElementById("map_canvas"), { 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    streetViewControl: false 
}); 

// Create the shared infowindow with two DIV placeholders 
// One for a text string, the other for the StreetView panorama. 
var content = document.createElement("div"); 
var title = document.createElement("div"); 
var boxText = document.createElement("div"); 

var myOptions = { 
     content: boxText 
     ,disableAutoPan: false 
     ,maxWidth: 0 
     ,pixelOffset: new google.maps.Size(-117,-200) 
     ,zIndex: null 
     ,boxStyle: { 
      background: "url('"+siteRoot+"images/house-icon-flat.png') no-repeat" 
      ,opacity: 1 
      ,width: "240px" 
      ,height: "190px" 
     } 
     ,closeBoxMargin: "10px 0px 2px 2px" 
     ,closeBoxURL: "http://kdev.langley.com/wp-content/themes/langley/images/close.png" 
     ,infoBoxClearance: new google.maps.Size(1, 1) 
     ,isHidden: false 
     ,pane: "floatPane" 
     ,enableEventPropagation: false 
}; 


var infoWindow = new InfoBox(myOptions); 
var MarkerImage = siteRoot+'images/house-web-marker.png'; 

// Create the markers 
for (index in markers) addMarker(markers[index]); 
function addMarker(data) { 
    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(data.lat, data.lng), 
     map: map, 
     title: data.title, 
     content: data.html, 
     icon: MarkerImage 
    }); 
    google.maps.event.addListener(marker, "click", function() { 
     infoWindow.open(map, this);   
     title.innerHTML = marker.getTitle(); 
     infoWindow.setContent(marker.content); 
     infoWindow.open(map, marker); 
     jQuery(".innerinfo").parent().css({'overflow':'hidden', 'margin-right':'10px'});    
    }); 
} 

// Zoom and center the map to fit the markers 
// This logic could be conbined with the marker creation. 
// Just keeping it separate for code clarity. 
var bounds = new google.maps.LatLngBounds(); 
for (index in markers) { 
    var data = markers[index]; 
    bounds.extend(new google.maps.LatLng(data.lat, data.lng)); 
} 
map.fitBounds(bounds); 
var origcent = new google.maps.LatLng(map.getCenter()); 
// Handle the DOM ready event to create the StreetView panorama 
// as it can only be created once the DIV inside the infowindow is loaded in the DOM. 

closeInfoWindow = function() { 
    infoWindow.close(); 
}; 

google.maps.event.addListener(map, 'click', closeInfoWindow); 

google.maps.event.addListener(infoWindow, 'closeclick', function() 
{ 
    centermap(); 
}); 

function centermap() 
{ 
    map.setCenter(map.fitBounds(bounds)); 
} 

} 

jQuery(window).resize(function() { 
googlemap(); 
}); 

任何幫助深表感謝。

回答

1

它值得看的兩件事情:

1)GTFS格式(https://developers.google.com/transit/gtfs/reference),這是一個基於CSV格式,讓別人交叉引用運輸時間和路線,如果你很幸運,那麼你的數據將爲你的運輸權威彙編:)

2)如果你可以拉動座標,那麼你可以做一個線條功能,只要你想(在瀏覽器容量範圍內)。這是在幾乎相同的方式進行,你在標記拉,除非你拉的功能,你想成爲一個折線: 像這樣(從gmaps文檔):

var flightPlanCoordinates = [ 
    new google.maps.LatLng(37.772323, -122.214897), 
    new google.maps.LatLng(21.291982, -157.821856), 
    new google.maps.LatLng(-18.142599, 178.431), 
    new google.maps.LatLng(-27.46758, 153.027892) 
    ]; 
    var flightPath = new google.maps.Polyline({ 
    path: flightPlanCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 

你會從PHP建立自己的陣列中,當然:)

希望有所幫助。

0

如果您使用免費的谷歌地圖,您可以使用起點,終點和8路航點。在谷歌地圖API首映,你可以使用23路點的路線車輛