2015-11-08 82 views
-1

按钮添加一个标记,以谷歌地图不工作

\t Long = -40.00; 
 
    Lati = 150.00; 
 
\t var map = new google.maps.Map(document.getElementById('map'), { 
 
\t \t zoom: 10, 
 
\t \t center: new google.maps.LatLng(Long, Lati), 
 
\t \t mapTypeId: google.maps.MapTypeId.ROADMAP 
 
\t }); 
 
    
 
    var marker = new google.maps.Marker({ 
 
\t \t position: new google.maps.LatLng(Long, Lati), 
 
\t \t map: map 
 
\t }); 
 
    
 
    function AddMarker() { 
 
    \t var Long = document.getElementById('longitude').value; 
 
     var Lati = document.getElementById('latitude').value; 
 
     
 
     var map = google.maps.Map(document.getElementById('map')); 
 

 
    \t var marker = new google.maps.Marker({ 
 
     \t position: new google.maps.LatLng(Long, Lati), 
 
\t \t \t map: map 
 
\t \t }); 
 
    }
<head> 
 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
 
    <title>Google Maps Multiple Markers</title> 
 
    <script src="http://maps.google.com/maps/api/js" 
 
      type="text/javascript"></script> 
 
</head> 
 
<body> 
 
<div> 
 
\t Long :<input type="text" id="longitude" size="10" value="-40.20"> 
 
    Lati :<input type="text" id="latitude" size="10" value="150.00"> 
 
    <input type="submit" onclick="AddMarker()" value="Add"> 
 
</div> 
 
    <div id="map" style="width: 500px; height: 400px;"></div> 
 
</body>


按钮添加一个标记不工作,任何IDEIA为什么呢?
如果我在var map变量上使用“new”,它将起作用,但它会创建并清除已存在的标记。

预先感谢

回答

1

下面是代码应如何样子,我删除这行代码是不必要的:

变种地图= google.maps.Map(的document.getElementById( '地图'));

JS:

var Long = -40.00; 
    var Lati = 150.00; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 10, 
     center: new google.maps.LatLng(Long, Lati), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 



function AddMarker() { 
     Long = document.getElementById('longitude').value; 
     Lati = document.getElementById('latitude').value; 


     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(Long, Lati), 
      map: map 
     }); 
    } 

小提琴: https://jsfiddle.net/wexd3spp/8/

+0

嗨......唉唉您删除以及初始化标志,因为它是没有意义的,我thatthere,但它不工作
好谢谢... – user3801128