javascript - 在谷歌地图中画线

标签 javascript google-maps-api-3

我尝试在谷歌地图中画一条线,我尝试了这段代码但没有任何反应,只有标记不在那里,我已经阅读了一些引用资料,如 https://developers.google.com/maps/documentation/javascript/v2/overlays#Icons_overview 并尝试代码,但线路仍然没有出来,任何人都可以帮助我吗? 下面是我的代码,假设地址是数组

for (var i = 0; i < address.length; i++) {
    geocoder.geocode({ 'address': address[i] }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            if (i == 2) {
                var pos1 = results[0].geometry.location;
                alert(pos1);
            }

            else if (i == 2) {
                var pos2 = results[0].geometry.location;
                alert(pos2);
            }

            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker
            (
                {
                    position: results[0].geometry.location,
                    map: map,
                    title: 'click me'
                }
            );
            var infowindow = new google.maps.InfoWindow
            (
                {
                    content: '<img src="http://wallpaperscraft.com/image/child_girl_emotion_sweet_face_25641_100x100.jpg"> Welcome to origineit'
                }
            );
            var flightPlanCoordinates = [
                new google.maps.LatLng(pos1),
                new google.maps.LatLng(pos2)
              ];

            if (i == 2) {

                var polyline = new google.maps.Polyline
                ({
                    path: flightPlanCoordinates,
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 3
                });
                polyline.setMap(map);
            }

            google.maps.event.addListener(marker, 'click', function () {
                // Calling the open method of the infoWindow 
                infowindow.open(map, marker);
            });
        }
        else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

最佳答案

这里的问题是,除了来自 V2 的多段线之外,您正在使用 Google Maps V3(您应该这样做)。您应该阅读 API documentation for Google Maps V3相反。

对于 Google Maps V3,它应该看起来像这样:

编辑

当测试你的代码时(而不是仅仅从我的脑海中写出一个答案)我还注意到你有一些范围界定问题(你没有解决检查 if(i == 2) 在您更新的代码中,我建议 this article 以便您了解有关该问题的更多信息)所以我也更改了它。下面的代码经过测试可以正常工作 ( http://jsfiddle.net/YMyc9/ ),但实际上应该对其进行一些重构以使其更易于理解。

var pos1 = null; // These need to be declared outside of the geocode-callback
var pos2 = null; // so we also remove 'var' from the assignment inside of that function
for (var i = 0; i < address.length; i++) {
    geocoder.geocode({'address': address[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            // We can't use 'i' here since this happens in a callback and the loop will already
            // have progressed and 'i' is (with my two test addresses) always 2 here.
            if (pos1 == null) {
                pos1 = results[0].geometry.location;
            }
            else if (pos2 == null) {
                pos2 = results[0].geometry.location;
            }
            else {
                /* We already got two so maybe quit? */
                return;
            }

            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map,
                title: 'click me'
            });
            var infowindow = new google.maps.InfoWindow({
                content: '<img src="http://wallpaperscraft.com/image/child_girl_emotion_sweet_face_25641_100x100.jpg"> Welcome to origineit'
            });

            /* NEW CODE HERE */
            if (pos2 != null) {
                var polyline = new google.maps.Polygon({
                    paths: [pos1, pos2],
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 3,
                    fillColor: '#FF0000',
                    fillOpacity: 0.35
                });

                polyline.setMap(map);
            } /* BACK TO THE OLD CODE HERE */

            google.maps.event.addListener(marker, 'click', function() {
                // Calling the open method of the infoWindow 
                infowindow.open(map, marker);
            });
        }
        else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

关于javascript - 在谷歌地图中画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14063743/

相关文章:

google-maps-api-3 - 圆形叠加层上的标题属性

javascript - Javascript动画的效率,clearRect()?

javascript - Vue.js 谷歌地图 API 与 @googlemaps/js-api-loader

javascript - Google map 接收多边形坐标

javascript - IE8 - 隐藏 div 内的表单,返回键不再起作用

javascript - 谷歌地图调用完成后运行函数

javascript - 关闭 Google map v3 中所有当前打开的信息窗口?

javascript - 上传图片时如何在vue组件上自动更新图片?

cookies - 如何使用javascript在用户本地存储上保存超过4kb的数据?

javascript - Node.js MongoDB .findOne 在查询字符串时返回最佳匹配