谷歌地图API Javascript.无法加载InfoWindow

Google Maps API Javascript. Cannot load the InfoWindow

本文关键字:加载 InfoWindow API Javascript 谷歌地图      更新时间:2023-09-26

我一直试图在标记上加载InfoWindow,但无法加载。我试图在用户单击标记后加载InfoWindow。问题是因为我没有在initialize()中定义eventListener吗?我试着做了,但还是没用!有人能帮助吗?

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);
}

当您在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/