2015-11-08 120 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