我在MAP初始化时创建了一个默认标记,但Marker没有显示内容?我能做些什么呢?

时间:2014-11-28 06:06:55

标签: javascript google-maps google-maps-api-3 map

我想在初始化地图标记的时候放置标记 但是infowindow的内容没有显示,我在JSFiddle.net上使用了相同的代码。 谷歌地图密钥是否有任何问题

function initialize()
{
	var map;
	var myCenter = new google.maps.LatLng(18.527362399999998,73.8783206); 
	var mapProp = {
  	center:myCenter,
    zoom:10,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map-canvas"),mapProp);
  addMarker(myCenter, 'Center of Map', map,"gg");
}


function addMarker(latlng,title,map,info){
    var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            zoom:12,
            title: title,
            draggable:true
    });

    	var infowindow = new google.maps.InfoWindow();
          //creating new infowindow
    	infowindow.close();//hide the infowindow
      	infowindow.setContent('Latitude: '); 
    	infowindow.open(map,marker);

}
<html>
  <body onload="initialize()"  style="background-color: #EFEFEF">
<div id="mapDiv" style="display: none;">
		     	<p>        
      				<input type="text" size="60" id="mapAdd" name="address" />
      				<input type="button" name="getLocation" value="Get Location" onclick="closeMap();"/>
      			</p>
				    <input type="text" id="lat"/>
				    <input type="text" id="lng"/>
				<div id="map-canvas"></div>
			</div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

你可以尝试这个 -

创建info

时,使用InfoWindow变量
var infowindow = new google.maps.InfoWindow(
'content' : info
);

但是我可以看到你使用了setContent,请检查z-index。

答案 1 :(得分:0)

问题:

  1. 您未包含Google Maps Javascript API v3
  2. 您的地图没有尺寸
  3. 工作代码段:

    function initialize()
    {
    	var map;
    	var myCenter = new google.maps.LatLng(18.527362399999998,73.8783206); 
    	var mapProp = {
      	center:myCenter,
        zoom:10,
        mapTypeId:google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map-canvas"),mapProp);
      addMarker(myCenter, 'Center of Map', map,"gg");
    }
    
    
    function addMarker(latlng,title,map,info){
        var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                zoom:12,
                title: title,
                draggable:true
        });
    
        	var infowindow = new google.maps.InfoWindow();
              //creating new infowindow
        	infowindow.close();//hide the infowindow
          	infowindow.setContent('Latitude: '); 
        	infowindow.open(map,marker);
    
    }
    <html>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
      <body onload="initialize()"  style="background-color: #EFEFEF">
    <div id="mapDiv">
    		     	<p>        
          				<input type="text" size="60" id="mapAdd" name="address" />
          				<input type="button" name="getLocation" value="Get Location" onclick="closeMap();"/>
          			</p>
    				    <input type="text" id="lat"/>
    				    <input type="text" id="lng"/>
    				<div id="map-canvas" style="height:500px; width:500px;"></div>
    			</div>
    
    </body>
    </html>