0

我想在我的地图中添加标记以及当前位置 两个程序在单独运行时都可以正常运行,但是在加入时,标记不仅可以正常工作 地图正在显示当前位置 我在下面放置了代码和两个程序的链接

第一个程序:Marker's fiddle

第二个程序:当前位置的小提琴

<title>Geolocation</title>
<style>
  html, body, #map {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
</style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>

<script>
var map;

function initialize() {
    var mapOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map'), mapOptions);

    // Try HTML5 geolocation
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var infowindow = new google.maps.InfoWindow({
                map: map,
                position: pos,
                content: 'Location found using HTML5.'
            });
            map.setCenter(pos);
        }, function() {
            handleNoGeolocation(true);
        });
    } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }
}

function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
    } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
    }
    var options = {
        map: map,
        position: new google.maps.LatLng(60, 105),
        content: content
    };
    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
    var locations = [
        ['Royal hall',39.032836,-94.576413, 4],
        ['Student Union',39.034182,-94.581661, 5],
        ['Law School',39.033226,-94.58215, 3],
        ['Bloch School',39.033515,-94.581063, 2],
        ['Cherry Street',39.036222,-94.58182, 1],
        ['Arts and Sciences',39.036857,-94.578483,6],
        ['ISAO',39.036061,-94.576912,7],
        ['Miller Nichols',39.035365,-94.576456,8],
        ['Flarsheim Hall',39.034349,-94.576386,9],
        ['Parking structure',39.032149,-94.57637,10],
    ];
    var marker, i;
    for (i = 0; i < locations.length; i++) {  
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][3], locations[i][2]),
            map: map
        });
    }
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

<body>
<div id="map"></div>

</body>

请帮我解决。

4

1 回答 1

2

实际的问题是您缺少将大括号括起来的handleNoGeolocation方法。

JSFiddle

检查上面的工作小提琴。

建议:

完成编写 Javascript 代码后,最好使用JSHint进行验证。

于 2013-10-23T04:12:48.873 回答