2016-12-24 15 views
-1

这个信息窗口这仅仅是我的Ajax调用的代码部分,我想包括点击事件标记位置:如何包含标记点击在阿贾克斯成功调用

var loaddata = function(place) { 
    var venue_id = place.venue_id; 
    var client_id = "LGHIIR3H5N4LB4X5GLRZOWCVWTP5DPAFBA4NZH02GG2BKWJE"; 
    var client_secret = "KY3VVWMVCCPZLDKVVYUSWJIZ0JXNZIHVCXX3S5PCRERYMEYP"; 
    var FoursquareUrl = "https://api.foursquare.com/v2/venues/" + venue_id + "?client_id=" + client_id + "&client_secret=" + client_secret + "&v=20130815"; 

    $.ajax({ 
     url: FoursquareUrl, 
     dataType: "json", 
     async: true 
    }).success(function(data) { 

     place.name = data.response.venue.name; 
     place.rating = data.response.venue.rating; 

     if (place.rating !== undefined) { 
      place.rating = data.response.venue.rating; 
     } else { 
      place.rating = 'Not Avaliable'; 
     } 
     console.log(place.name); 
     console.log(place.rating); 
     var image_prefix = data.response.venue.bestPhoto.prefix; 
     var image_suffix = data.response.venue.bestPhoto.suffix; 

     console.log(image_prefix + "320x200" + image_suffix); 
     var imag = image_prefix + "320x200" + image_suffix; 


     var infowindow = new google.maps.InfoWindow(); 


     infowindow.setContent('<div>' + '<h5>' + place.name + '</h5>' + ' <p>' + place.rating + '</p>' + "<img src=" + imag + ">" + '</div>'); 
     infowindow.open(map, place.marker); 
     new google.maps.event.trigger(place.marker, 'click'); 

     //can i include marker click event 

    }).fail(function(error) { 
     alert('Failed to get FOURSQUARE api'); 
    }); 

}; 

在这里,我需要证明我的infowindow当标记点击时。我可以在Ajax成功中包含点击事件吗?

+0

在这里,我需要证明我的信息窗口时,获得点击的标记,我可以包括点击阿贾克斯成功 –

+0

还是什么对内部事件在包含loadata()的同时传入我的点击事件 –

+0

标记定义在哪里? – geocodezip

回答

0

一个简单的方法是使用clousure的(在这种情况下addListenersOnMyMarker功能)

var loaddata = function(place) { 
    var venue_id = place.venue_id; 
    var client_id = "LGHIIR3H5N4LB4X5GLRZOWCVWTP5DPAFBA4NZH02GG2BKWJE"; 
    var client_secret = "KY3VVWMVCCPZLDKVVYUSWJIZ0JXNZIHVCXX3S5PCRERYMEYP"; 
    var FoursquareUrl = "https://api.foursquare.com/v2/venues/" + venue_id + "?client_id=" + client_id + "&client_secret=" + client_secret + "&v=20130815"; 

    $.ajax({ 
     url: FoursquareUrl, 
     dataType: "json", 
     async: true 
    }).success(function(data) { 

     place.name = data.response.venue.name; 
     place.rating = data.response.venue.rating; 

     if (place.rating !== undefined) { 
      place.rating = data.response.venue.rating; 
     } else { 
      place.rating = 'Not Avaliable'; 
     } 
     console.log(place.name); 
     console.log(place.rating); 
     var image_prefix = data.response.venue.bestPhoto.prefix; 
     var image_suffix = data.response.venue.bestPhoto.suffix; 

     console.log(image_prefix + "320x200" + image_suffix); 
     var imag = image_prefix + "320x200" + image_suffix; 


     var infowindow = new google.maps.InfoWindow(); 


     var marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map, 
      }); 


     infowindow.setContent('<div>' + '<h5>' + place.name + '</h5>' + ' <p>' + place.rating + '</p>' + "<img src=" + imag + ">" + '</div>'); 
     //infowindow.open(map, place.marker); 
     //new google.maps.event.trigger(place.marker, 'click'); 


     var addListenersOnMyMarker = function(map, marker, inforwindo) { 
      google.maps.event.addListener(marker, 'click', function (event) { 
      infowindow.open(map, marker); 
      }); 
     }; 

     addListenersOnMyMarker(aMarker, aInfoIfoWindow); 


     //can i include marker click event 

    }).fail(function(error) { 
     alert('Failed to get FOURSQUARE api'); 
    }); 

};