在谷歌地图上显示信息框中标记的标题

时间:2014-05-20 10:04:09

标签: javascript google-maps

我想显示谷歌地图上有标记的每个地方的标题(地名)。 目前是显示坐标

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

但我想让它显示"标题"这个地方。 我该怎么做呢 我的代码是:

    <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>

1 个答案:

答案 0 :(得分:2)

使用title作为marker-property并在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);
});