InfoWindow not Displaying

后端 未结 4 659
谎友^
谎友^ 2021-01-28 08:54

I\'m trying to have this so that someone clicks on the map, and an infoWindow pops up telling them that latlng. For now, just trying to figure out how to get an infoWindow to co

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-28 09:21

    You were correct in passing the LatLng of the event (event.latLng), not the whole event. To help with the output, I included code that splits the lat and lng up into strings. A couple other things I fixed: needs to be a capital "I" in google.maps.InfoWindow; set the content to new latLng if the window already exists; added the code to actually open the window at the end. This should take care of everything for ya

    function InfoWindow(location) {
        var lat = location.lat().toString();
        var lng = location.lng().toString();
        var new_content = "Lat: " + lat + "
    Long: " + lng if ( marker ) { marker.setPosition(location); marker.setContent(new_content); } else { marker = new google.maps.InfoWindow({ position: location, content: new_content }); } marker.open(map); } google.maps.event.addListener(map, 'click', function(event) { InfoWindow(event.latLng); });

提交回复
热议问题