开发者

Google Maps API: undefined marker title

开发者 https://www.devze.com 2023-04-12 23:45 出处:网络
I\'m working on a map where the user can click unpon, generating new markers. For each marker, some info should be displayed in the sidebar: latitude, longitude and a title.

I'm working on a map where the user can click unpon, generating new markers. For each marker, some info should be displayed in the sidebar: latitude, longitude and a title. The title is generated by reverse geocoding. I have created an array of markers and everything seems to be ok, but the title the first marker in the array is always "undefined". No problems with coords: just the first element's title! The title is regularly shown in the infowindow... But it is "undefined" in the first element of the array. Here is my code:

<script>
var map;
var markers = [];
var rome = new google.maps.LatLng(41.9012, 12.4751);
var infowindow;
var geocoder;

function loadMap(){
var myOptions = {
    zoom: 15,
    center: rome,
    mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map( document.getElementById('canvas'), myOptions );
google.maps.event.addListener(map, 'click', function(evt){
    addMarker( evt.latLng );
});
}

function addMarker( location ){
var marker = new google.maps.Marker({
    position: location,
    map: map
});
marker.setMap( map );
openInfoWindow( marker );

markers.push( marker );
updateList( marker );
}

function openInfoWindow( marker ){
geocoder = new google.maps.Geocoder();
geocoder.geocode({'location': marker.getPosition()}, function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {
        address = results[0].formatted_address;
    }else{
        address("Address not found");
    }
    var infowindow = new google.maps.InfoWindow({
        content: address
    });
    infowindow.open(map, marker);
    marker.setTitle(address);
    console.log(address);
}); 
}

function updateList( marker ){
    var lat, lng, title;
    if( markers.length > 0 ){
    $('#sidebar').html('<ul></ul>');

    for( var i = 0; i < markers.length; i++ ){
        lat = markers[i].getPosition().lat();
        lng = markers[i].getPosition().lng();
        title = markers[i].getTitle();
        var html = '<li>Lat: ' + lat + '<br />Lng: ' + lng + '<br />' + title + '</li>';

        $(html).prependTo('#sidebar ul');

    }//end for
    }//end if
}

$(document).ready( loadMap );
</script>

Wh开发者_StackOverflow中文版ere do I wrong?


I've found the solution!

function addMarker( location ){
geocoder = new google.maps.Geocoder();
geocoder.geocode({'location': location}, function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {
        address = results[0].formatted_address;
    }else{
        address("Address not found");
    }
    var marker = new google.maps.Marker({
        position: location,
        title: address,
        map: map
    });
    google.maps.event.addListener(marker, 'click', function(){
        openInfoWindow(marker);
    });
    marker.setMap( map );
    openInfoWindow(marker);

    markers.push(marker);

    updateList( markers );

}); 
}
function openInfoWindow( marker ){
var title = marker.getTitle();
if(infowindow){
    infowindow.close();
}
infowindow = new google.maps.InfoWindow({
        content: title
    });
    infowindow.open(map, marker);
}

In the first block of code I called the geocoder from within openInfoWindow() function. In the second block of code, I called the geocoder from addMarker() function. Oh, I set up the title of the marker in the maker declaration, inside addMarker() function. I'm not sure about the reason, but now it works well!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号