2011-06-08 42 views
-1

錯誤:
latlng is undefined(var lng = latlng.lng();)儘管它得到的值,並把在文本框 沒有從Ajax響應。我想要作爲文本的響應,而不是xml。我錯過了什麼?latlang沒有定義,沒有從Ajax響應在谷歌地圖

function load() { 
if (GBrowserIsCompatible()) { 
map = new GMap2(document.getElementById("map")); 
map.addControl(new GSmallMapControl()); 
//map.addControl(new GMap2TypeControl()); 
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom); 

GEvent.addListener(map, "click", function(overlay, latlng) { 
var inputForm = document.createElement("form"); 
inputForm.setAttribute("action",""); 
inputForm.onsubmit = function() {storeMarker(); return false;}; 
//retrieve the longitude and lattitude of the click point 
var lng = latlng.lng(); 
var lat = latlng.lat(); 
inputForm.innerHTML = '<fieldset style="width:150px;">' 
+ '<legend><b>Add Property<b></legend>' 
+ '<label for="name"><B>Name</b></label>' 
+ '<input type="text" id="name" style="width:100%;"/>' 
+ '<label for="address"><b>Address</b></label>' 
+ '<input type="text" id="address" style="width:100%;"/>' 
+ '<label for="address"><b>Type</b></label>' 
+'<select id="type"><option>Homes</option><option>Plots</option><option>Commercials</option></select>' 
+ '<label for="address"><b>Prperty Type</b></label>' 
+'<select id="property_type"><option>Sale</option><option>Rent</option><option>Wanted</option></select>' 
+ '<label for="address"><b>Descreption</b></label>' 
+ '<textarea id="description" cols="15" rows="4" name="description"></textarea>' 
+ '<input type="text" id="longitude" value="' + lng + '"/>' 
+ '<input type="text" id="latitude" value="' + lat + '"/>' 
+ '<input type="submit" value="ADD"/>' 
+ '</fieldset>'; 

map.openInfoWindow (latlng,inputForm); 
}); 
} 
} 

這裏是商店的標記功能:

function storeMarker(){ 
//alert("xainee"); 
var lng = document.getElementById("longitude").value; //getting the longitude 
var lat = document.getElementById("latitude").value;//getting the latitude 

alert(lng +"and"+lat); 
//geeting the user data in form 
var getVars = "storeMarker.php?name=" + document.getElementById("name").value 
+ "&address=" + document.getElementById("address").value 
+ "&description=" + document.getElementById("description").value 
+ "&property_type=" + document.getElementById("property_type").value 
+ "&type=" + document.getElementById("type").value 
+ "&lng=" + lng 
+ "&lat=" + lat ; 

//alert(getVars); 
////////////////////////////////////////// 
var Request = false; 
if (window.XMLHttpRequest) { 
    Request = new XMLHttpRequest(); 
} else if (window.ActiveXObject) { 
    Request = new ActiveXObject("Microsoft.XMLHTTP"); 
} 

if (Request) { 
    alert("workinh"); 
    Request.open("GET","storeMarker.php"+getVars,true); 

    Request.onreadystatechange=function() 
    { 
    if (Request.readyState==4 && Request.status==200) 
    { 
    document.write(responseText); 
    } 
    } 
    Request.send(null); 
} 

} 

這裏是storeMarker.php頁:

<?php 
include_once('clsGeneral.php'); 
echo $name=$_GET['name']; 
echo $address=$_GET['address']; 
echo $type=$_GET['type']; 
echo $property_type=$_GET['property_type']; 
echo $descreption=$_GET['description']; 
echo $lat=(float)$_GET['lat']; 
echo $lan=(float)$_GET['lng']; 

$my_query="INSERT INTO map_marker SET name='$name', 
    address='$address',type='$type',property_address='$property_type', 
    descreption='$descreption',lat='$lat',lan='$lat'"; 
db_execute($my_query); 
?> 

我的問題是,當Geventlistner通過點擊燒成,彈出呈現出在文本框中有經度和緯度值的所有元素,但是當我點擊任何文本框時,它顯示錯誤「latlan未定義」。其次它不發送請求,例如它發送比沒有響應在這裏我想在字符串中不響應XML。

+0

您也似乎在同一時間詢問多個問題(一個是Google Maps API問題,另一個是看似不相關的AJAX問題)。如果分別詢問每個問題,StackOverflow的效果最佳。 – 2011-06-08 23:23:08

回答

4

根據the fine Google Maps manual

在地圖API事件系統中的許多事件中傳遞參數時,該事件被觸發。例如,GMap2「點擊」事件通過overlayoverlaylatlng如果地圖點擊發生在覆蓋;否則,它通過地圖座標的latlng。您可以通過將指定的符號直接傳遞給事件偵聽器中的函數來訪問這些參數。

在下面的示例中,我們通過檢查latlng參數是否定義來確保點擊在地圖上的第一個測試;如果是這樣,我們在點擊的座標上方打開一個信息窗口,並顯示與縮放級別一起轉換爲像素空間的座標。

+1

SIR!我沒有越來越多的人談論?? plz讓我知道我的代碼,如何改變我的代碼來得到我想要的 – 2011-06-09 18:49:11

+0

@Xainee Khan:我不知道怎麼說比Google更清楚。當發生'click'事件時,'latlng'參數可能是** undefined **。你需要處理這種情況。我鏈接到的頁面顯示瞭如何執行此操作的示例。你是否期待我爲你寫代碼? – 2011-06-09 20:14:03

+0

不需要寫一個代碼我只是想提到thanx BTW – 2011-06-09 20:54:08

相關問題