2012-02-03 140 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路点的路线车辆