简体   繁体   中英

show title of marker in infobox on google map

I want to show the title (place name) of each place where there is a marker on the google map. At the moment is is showing the co-ordinates

infowindow.setContent("coord:"+dmarker.getPosition().toUrlValue(6));

but I want it to show the "title" of the place. How do I do this My code is:

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
   <script type="text/javascript">
   var map = null;
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
   var markers = [
                        { 
                             "title": <?php echo "'" . $info['name']  . "'"; ?>,
                             "lat": <?php echo "'" . $info['lat'] . "'"; ?>,
                             "lng": <?php echo "'" . $info['long'] . "'"; ?>,
                        },


            {
               "title": 'Kinar Hotel',
               "lat": '32.8617740',
               "lng": '35.6452770', 
             }
   ];
   </script>


<script type="text/javascript">
function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(
            parseFloat(markers[0].lat),
            parseFloat(markers[0].lng)),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var path = new google.maps.MVCArray();
    var service = new google.maps.DirectionsService();

    var infoWindow = new google.maps.InfoWindow();
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    var poly = new google.maps.Polyline({
        map: map,
        strokeColor: '#F3443C'
    });
    var lat_lng = new Array();

    /* path.push(new google.maps.LatLng(parseFloat(markers[0].lat),
                                     parseFloat(markers[0].lng)));
    */
    var marker = new google.maps.Marker({
                   position:map.getCenter(), 
                   map:map 
                 });

    bounds.extend(marker.getPosition());
google.maps.event.addListener(marker, "click", function(evt) {
    infowindow.setContent("coord:"+marker.getPosition().toUrlValue(6));
    infowindow.open(map,marker);
});            
    for (var i = 0; i < markers.length; i++) {
        if ((i + 1) < markers.length) {
            var src = new google.maps.LatLng(parseFloat(markers[i].lat), 
                                             parseFloat(markers[i].lng));
            var smarker = new google.maps.Marker({position:src, map:map});
            bounds.extend(smarker.getPosition());
    google.maps.event.addListener(smarker, "click", function(evt) {
       /*  infowindow.setContent("coord:"+smarker.getPosition().toUrlValue(6));*/
        infowindow.open(map,smarker);
    });            
            var des = new google.maps.LatLng(parseFloat(markers[i+1].lat), 
                                             parseFloat(markers[i+1].lng));
            var dmarker = new google.maps.Marker({position:des, map:map});
            bounds.extend(dmarker.getPosition());
    google.maps.event.addListener(dmarker, "click", function(evt) {
        /* infowindow.setContent("coord:"+dmarker.getPosition().toUrlValue(6));*/
        infowindow.open(map,dmarker);
    });            
            //  poly.setPath(path);
            service.route({
                origin: src,
                destination: des,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, function (result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                        path.push(result.routes[0].overview_path[i]);
                    }
                    poly.setPath(path);
                    map.fitBounds(bounds);
                }
            });
        }
    }
} 
google.maps.event.addDomListener(window,'load',initialize);
   </script>

Use the title as marker-property and access this property in the click-handler:

var dmarker = new google.maps.Marker({position:des, 
                                      map:map,
                                      title:markers[i+1].title
                                     });

google.maps.event.addListener(dmarker, "click", function(evt) {  
    infowindow.setContent(this.get('title'));
    infowindow.open(map,this);
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM