如何在此Google地图代码中添加标记?

时间:2011-01-30 01:51:39

标签: javascript google-maps-api-3

我想在地理编码位置添加标记,我该怎么做?

        function setMapAddress( address )
        {
            var geocoder = new google.maps.Geocoder();

            geocoder.geocode( { address : address }, function( results, status ) {
                if( status == google.maps.GeocoderStatus.OK ) {
                    var latlng = results[0].geometry.location;
                    var options = {
                        zoom: 8,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };

                    new google.maps.Map( document.getElementById( 'map_canvas2' ), options ); 

                }
            } );
        }

        setMapAddress( "{/literal}{$listing.City},{$listing.State}{literal}" );

由于

1 个答案:

答案 0 :(得分:1)

添加变量以保存对地图对象的引用,然后添加标记:

var map = new google.maps.Map( document.getElementById( 'map_canvas2' ), options );
var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
    title: "Your location."
});