2016-01-04 69 views
0

我目前正在編寫一個旅行網站,該網站將允許用戶單擊地圖,導致標記進入該位置。從這裏我希望用戶能夠將信息窗口應用到具體的標記,例如國家,城市,持續時間,評論等細節。但是,當我編碼時,信息窗口會出現,但數據不是唯一的,用戶輸入並保存的數據在下一個標記上顯示完全相同。舉例來說,用戶點擊愛爾蘭,當他們點擊英格蘭時,去編輯該標記,標記將具有所有愛爾蘭信息。我如何使這個獨特?向標記添加獨特的信息窗口並更新數據庫以存儲信息(谷歌地圖api)

這裏是我的HTML代碼:

function initialize() { 
    var latlng = new google.maps.LatLng(54.5, -7.0); 
    var options = { 
    zoom: 7, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(document.getElementById("map"), options); 
    var html = "<table>" + 
      "<tr><td>Country:</td> <td><input type='text' id='country'/> </td> </tr>" + 
      "<tr><td>City:</td> <td><input type='text' id='city'/></td> </tr>" + 
      "<tr><td>Duration:</td> <td><input type='text' id='duration'/></td> </tr>" + 
      "<tr><td>Category:</td> <td><select id='category'>" + 
       "<option value='city' SELECTED>City Break</option>" + 
       "<option value='beach'>Beach Holiday</option>" + 
       "<option value='romantic'>Romantic Holiday</option>" + 
       "<option value='activity'>Activity Holiday</option>" + 
       "<option value='site'>Site Seeing</option>" + 
       "</select> </td></tr>" + 
      "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>"; 
infowindow = new google.maps.InfoWindow({ 
content: html 
}); 

google.maps.event.addListener(map, "click", function(event) { 
    marker = new google.maps.Marker({ 
     position: event.latLng, 
     map: map 
    }); 
    google.maps.event.addListener(marker, "click", function() { 
     infowindow.open(map, marker); 
    }); 
}); 
} 

function saveData() { 
    var country = escape(document.getElementById("country").value); 
    var duration = escape(document.getElementById("duration").value); 
    var category = document.getElementById("category").value; 
    var latlng = marker.getPosition(); 

    var url = "phpsqlinfo_addrow.php?country=" + country + "&city" + city + "&duration=" + duration + 
      "&category=" + category + "&lat=" + latlng.lat() + "&lng=" + latlng.lng(); 
    downloadUrl(url, function(data, responseCode) { 
    if (responseCode == 200 && data.length >= 1) { 
     infowindow.close(); 
     document.getElementById("message").innerHTML = "Location added."; 
    } 
    }); 
} 

function downloadUrl(url, callback) { 
    var request = window.ActiveXObject ? 
     new ActiveXObject('Microsoft.XMLHTTP') : 
     new XMLHttpRequest; 

    request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     request.onreadystatechange = doNothing; 
     callback(request.responseText, request.status); 
    } 
    }; 

    request.open('GET', url, true); 
    request.send(null); 
} 

function doNothing() {} 
</script> 

回答

0

重新初始化當你創建一個新的標記信息窗口內容:

google.maps.event.addListener(map, "click", function(event) { 
    marker = new google.maps.Marker({ 
     position: event.latLng, 
     map: map 
    }); 
    infowindow.setContent(html); 
    google.maps.event.addListener(marker, "click", function() { 

     infowindow.open(map, marker); 
    }); 
}); 
+0

是擺脫了,多虧問題的,但是現在發生的事情我當是再次點擊標記,它會顯示最近標記的信息框?我如何阻止這個,以便它只打開標記信息框,我點擊 –

+0

這是另一個問題。保存與每個標記相關聯的數據,以便在單擊它時可以重新顯示,或者在將信息提交到數據庫時刪除標記。 – geocodezip

+0

對不起,我是一個新手,我將如何去保存數據,以便它可以重新顯示? –