我无法看到我的Google地图

时间:2018-04-29 22:11:25

标签: api maps

嗨大家:)我的谷歌地图有些问题,我不知道为什么我看不到它们。有人能告诉我我做错了什么吗?这是我的代码:

jQuery(document).ready( function($){    

        var geocoder;
        var map;
        var markersArray = [];
        var infos = [];

        geocoder = new google.maps.Geocoder();
        var myOptions = {
              zoom: 9,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        // map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var bounds = new google.maps.LatLngBounds();
        var encodedString;
        var stringArray = [];
        encodedString = document.getElementById("encodedString").value;
        stringArray = encodedString.split("****");

        var x;
        for (x = 0; x < stringArray.length; x = x + 1)
        {
            var addressDetails = [];
            var marker;
            addressDetails = stringArray[x].split("&&&");

            var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]);
            //alert(image + " " + addressDetails[1] );
            marker = new google.maps.Marker({ 
                map: map, 
                position: lat,
                content: addressDetails[0]
            });
            markersArray.push(marker);
            google.maps.event.addListener( marker, 'click', function () {
                closeInfos();
                var info = new google.maps.InfoWindow({content: this.content});
                info.open(map,this);
                infos[0]=info;
            });
            bounds.extend(lat);
        }

        map.fitBounds(bounds);              

        function closeInfos(){
                    if(infos.length > 0){
                           infos[0].set("marker",null);
                           infos[0].close();
                           infos.length = 0;
                    }
}

    });

以下是代码链接:https://jsfiddle.net/w1zst7uz/3/

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  1. 这一行

    encodedString = document.getElementById("encodedString").value;
    

    产生以下错误

      

    TypeError:document.getElementById(...)为null

    页面上没有id =“encodedString”的元素。

  2. 此外,myOptions中缺少中心。这是必需的属性(见Map Options

    myOptions应该看起来像这样

    var myOptions = {
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: {lat: -25.363, lng: 131.044}                                
    }