小编Qua*_*aye的帖子

使用Google Maps API添加标记功能

我有以下Javascript,其中包括标准的Google Maps API initialize()函数和自定义addMarker()函数.地图将加载正常,但标记不会添加到地图中.

<script type="text/javascript">

    // Standard google maps function
    function initialize() {
        var myLatlng = new google.maps.LatLng(40.779502, -73.967857);
        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }

    // Function for adding a marker to the page.
    function addMarker(location) {
        marker = new google.maps.Marker({
            position: location,
            map: map
        });
    }

    // Testing the addMarker function
    CentralPark = new google.maps.LatLng(37.7699298, -122.4469157);
    addMarker(CentralPark);

</script>
Run Code Online (Sandbox Code Playgroud)

javascript google-maps-api-3 google-maps-api-2

27
推荐指数
2
解决办法
11万
查看次数