Google Maps API Javascript。无法加载InfoWindow

问题描述:

我一直在试图加载InfoWindow的标记,但我无法加载它。我想在用户点击标记后加载InfoWindow。问题是因为我没有在intialize()中定义eventListener?我尝试过,但仍然无效!任何人都可以帮助Google Maps API Javascript。无法加载InfoWindow

的JavaScript代码是:

var zwsid = "*My_API_Key*"; 
var request = new XMLHttpRequest(); 
var mapObj; 
var map_Estate; 
function initialize() 
{ 
    geocoder = new google.maps.Geocoder(); 
    var defaultLoc = new google.maps.LatLng(51.5286416, -0.1015987); //defines location of the Map 
    map_Estate =  // defines the properties of the map 
    { center: defaultLoc, 
    zoom:16, 
    mapTypeId: google.maps.MapTypeId.ROADMAP } 
    mapObj = new google.maps.Map(document.getElementById('mapArea'), map_Estate); //creates a object to load in HTML file 
} 

function xml_to_string (xml_node) 
{ 
    var value1, marker; 
    //document.getElementById("test1").innerHTML = "Entered addrMap"; 
    var address = document.getElementById('address').value; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) 
    { 
     document.getElementById("test1").innerHTML = " LATLNG is: "+ results[0].geometry.location+ " STATUS: " + status; 
      mapObj.setCenter(results[0].geometry.location); 
      marker = new google.maps.Marker({ 
               map: mapObj, 
               position: results[0].geometry.location });     
    } 
    else 
    { alert('Geocode was not successful for the following reason: ' + status); } 
    });  
     google.maps.event.addListener(marker, 'click', function() 
    { 
     var infoProp = new google.maps.InfoWindow({ content: "Value of the property is "}); 
     infoProp.open(mapObj, marker); 
});   

if (xml_node.xml) 
value1 = xml_node.xml; 
var xml_serializer = new XMLSerializer(); 
value1 = xml_serializer.serializeToString(xml_node); 
document.getElementById("text1").innerHTML += "City: "+document.getElementById("city").value + 
               ", State: "+document.getElementById("state").value+ ", Cost: "+value1+"<br></br>"; 
} 

function displayResult() 
{ 
    if (request.readyState == 4) 
    { 
    var xml = request.responseXML.documentElement; 
    var value = xml.getElementsByTagName("zestimate")[0].getElementsByTagName("amount")[0];   
    //document.getElementById("output").innerHTML = xml_to_string(value); 
    xml_to_string(value); 
} 
} 

function sendRequest() { 
request.onreadystatechange = displayResult; 
var address = document.getElementById("address").value; 
var city = document.getElementById("city").value; 
var state = document.getElementById("state").value; 
var zipcode = document.getElementById("zipcode").value; 
request.open("GET","proxy.php?zws-id="+zwsid+"&address="+address+"&citystatezip="+city+"+"+state+"+"+zipcode); 
request.withCredentials = "true"; 
request.send(null); 
} 
+0

添加一个简单的jsfiddle,使我们更容易帮助你。 – 2pha 2014-09-28 06:16:07

+0

请用你的html和xmlfile更新这个小提琴。谢谢。 http://jsfiddle.net/iambnz/mmy15svu/ – devbnz 2014-09-28 12:36:49

+0

@bnz我已经用HTML更新了jsfiddle文件。基本上,我试图实现的是通过使用Zillow API,我试图提取物业的价格。所以,xml文件与API连接。 – user2607744 2014-09-28 17:47:45

当您移动监听器里的if语句,它会工作。

除此之外,我建议清理和结构代码。

if语句(JS)

if (status == google.maps.GeocoderStatus.OK) 
    { 
     document.getElementById("test1").innerHTML = " LATLNG is: "+ results[0].geometry.location+ " STATUS: " + status; 
      mapObj.setCenter(results[0].geometry.location); 
      marker = new google.maps.Marker({ 
               map: mapObj, 
               position: results[0].geometry.location }); 

google.maps.event.addListener(marker, 'click', function() 
    { 
     var infoProp = new google.maps.InfoWindow({ content: "Value of the property is "}); 
     infoProp.open(mapObj, marker); 
});   
    } 

工作小提琴这里:http://jsfiddle.net/iambnz/z1dx45rL/

+0

丹科!有效!当然,我会重组代码! – user2607744 2014-09-28 22:20:54

+0

你能告诉我这是什么意思:'函数xml_to_string(xml_node){var value1; if(xml_node.xml)value1 = xml_node.xml; var xml_serializer = new XMLSerializer(); value1 = xml_serializeToString(xml_node);' – user2607744 2014-09-29 05:00:06

+0

有些值来自xml文件。不知道这些是否是标记 - 或者你的其他api(zillow)。你可以分享这些XML文件之一吗? – devbnz 2014-09-29 10:29:26