添加Google地图的路径(API v3)

时间:2021-04-11 15:26:19

I'm trying to use the Google Maps API v3 to display a map on my website. I want it to display some bus lines of my city. It should look like a colored path (e.g. a red path for line 1) which displays the trip the bus makes.

我正在尝试使用Google Maps API v3在我的网站上显示地图。我希望它能显示我所在城市的一些公交线路。它应该看起来像一条彩色路径(例如第1行的红色路径),它显示了公交车的行程。

This is what I have till now: http://dejoridavid.pe.hu/sasabus/map.php. On the map the markers display the locations of the busses, only the line paths are missing. How can I add a path to my map using the Google Maps API v3? It shall be a path going from A to B to C and so on, then reaching A again.

这就是我现在所拥有的:http://dejoridavid.pe.hu/sasabus/map.php。在地图上,标记显示总线的位置,仅缺少线路径。如何使用Google Maps API v3为我的地图添加路径?它应该是从A到B到C的路径,依此类推,然后再到达A.

1 个解决方案

#1


Taken from the Google Maps API docs (I added comments to clarify what is happening)

取自Google Maps API文档(我添加了评论以澄清发生了什么)

// initialize a mapOptions object
var mapOptions = {
    zoom: 3,
    center: new google.maps.LatLng(0, -180),
    mapTypeId: google.maps.MapTypeId.TERRAIN
};

// initialize a map object
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

// initialize an array of LatLng objects. These are your markers in your city
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)];

// initialize a Polyline object. You can set the color, width, opacity, etc. 
var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
});

// set the polyline's map with your map object from above.
flightPath.setMap(map);

#1


Taken from the Google Maps API docs (I added comments to clarify what is happening)

取自Google Maps API文档(我添加了评论以澄清发生了什么)

// initialize a mapOptions object
var mapOptions = {
    zoom: 3,
    center: new google.maps.LatLng(0, -180),
    mapTypeId: google.maps.MapTypeId.TERRAIN
};

// initialize a map object
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

// initialize an array of LatLng objects. These are your markers in your city
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)];

// initialize a Polyline object. You can set the color, width, opacity, etc. 
var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
});

// set the polyline's map with your map object from above.
flightPath.setMap(map);