最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to join all markers with paths in Google Maps? - Stack Overflow

programmeradmin6浏览0评论

I'm new to Google Maps (API) and I need to get the following result:

At the moment, I know how to render the map and place markers on it (based on longitude and latitude).

var map;

map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 7,
    center: new google.maps.LatLng(response[0].latitude, response[0].longitude),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

for (var i = 0; i < response.length; ++i) {

    new google.maps.Marker({
        'map' : map,
        'position' : new google.maps.LatLng(response[i].latitude, response[i].longitude),
        'title' : response[i].address
    });

}

Variable response structure is like:

[
Object
address: "Krišjāņa Barona iela 25, Riga, LV-1011, Latvia"
latitude: "24.1245290"
longitude: "56.9528510"
__proto__: Object
, 
Object
address: "Rīgas iela 1, Tukums, Tukuma novads, LV-3101, Latvia"
latitude: "23.1630590"
longitude: "56.9663880"
__proto__: Object
]

There could be a lot of markers. I'm looking for a way to join markers with paths like in the preview image.

I don't know for what I should search and I need your help in this, guys. Thanks in an advice!

I'm new to Google Maps (API) and I need to get the following result:

At the moment, I know how to render the map and place markers on it (based on longitude and latitude).

var map;

map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 7,
    center: new google.maps.LatLng(response[0].latitude, response[0].longitude),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

for (var i = 0; i < response.length; ++i) {

    new google.maps.Marker({
        'map' : map,
        'position' : new google.maps.LatLng(response[i].latitude, response[i].longitude),
        'title' : response[i].address
    });

}

Variable response structure is like:

[
Object
address: "Krišjāņa Barona iela 25, Riga, LV-1011, Latvia"
latitude: "24.1245290"
longitude: "56.9528510"
__proto__: Object
, 
Object
address: "Rīgas iela 1, Tukums, Tukuma novads, LV-3101, Latvia"
latitude: "23.1630590"
longitude: "56.9663880"
__proto__: Object
]

There could be a lot of markers. I'm looking for a way to join markers with paths like in the preview image.

I don't know for what I should search and I need your help in this, guys. Thanks in an advice!

Share Improve this question asked Oct 25, 2011 at 15:31 daGrevisdaGrevis 21.3k39 gold badges103 silver badges139 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

An example from Google's tutorial:

  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
  });

Also, see the reference entry for Polylines.

If you don't know how to map the response object to an array of LatLng objects, here's an example:

var flightPath = responseArray.map(function (item) {
    return new google.maps.LatLng(item.latitude, item.longitude);
});
发布评论

评论列表(0)

  1. 暂无评论