使用Google地图绘制公交路线

时间:2012-02-03 20:53:35

标签: javascript google-maps

我正在租赁物业。在网站上,我想要一张谷歌地图,标记所有属性,并绘制本地巴士路线,以便租户可以看到物业与路线的接近程度。

我已经解决了问题的第一部分;我用标记绘制了属性。现在我需要添加公交路线。

我已经看过这个,但我无法找到实现它的最佳方法。我查看折线并使用this tool,但路线很复杂,需要数百个坐标。

有某种路线api,如this post,但显然它只能占用8个航路点。是吗?

理想情况下,我想通过选择一个起点,一个终点并将路线拖到适当位置来绘制地图;然后以某种方式将该路线导入我所拥有的路线。

以下是我要导入的确切路线: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();
});

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

值得看看两件事:

1)GTFS格式(https://developers.google.com/transit/gtfs/reference)这是一种基于csv的格式,允许某人跨越参考传输时间和路线,如果您幸运的话,那么数据将为您的运输权限而组装:)

2)如果您可以拉入坐标,那么您可以根据需要制作线要素(在浏览器容量的范围内)。除了将所需要的特征拉入折线外,这与拉入标记的方式大致相同: 像这样(来自gmaps docs):

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构建你的数组,当然:)

希望有所帮助。

答案 1 :(得分:0)

如果您使用免费的谷歌地图,您可以使用起点,终点和8路点。在google maps api premiere中,您可以使用23路点来路由车辆