2014-01-13 132 views
0

我已經閱讀了一堆這裏的帖子,但似乎無法弄清楚我做錯了什麼。有關如何添加鏈接而不是從Google地圖之外打開infowindow的任何建議?打開InfoWindow with點擊鏈接後,谷歌地圖API

我已經試過......

<a href="javascript:google.maps.event.trigger(gmarkers[0],'click');">Open Info Window</a> 

function infoOpen(i) {google.maps.event.trigger(gmarkers[i], 'click');} 

,但沒有運氣!

var locations = [ 
    <?php $i = 1; while (have_posts()) : the_post(); ?> 
     <?php if (get_geocode_latlng($post->ID) !== '') : ?> 
      { 
       latlng : new google.maps.LatLng<?php echo get_geocode_latlng($post->ID); ?>, 
       info : $('#item<?php echo $i; ?>').clone().get(0), 

     }, 
     <?php endif; ?> 
    <?php $i++; endwhile; ?> 
]; 


// Enable the visual refresh 
google.maps.visualRefresh = true; 
var number = '1'; 
var infowindow = new google.maps.InfoWindow({maxWidth: 200}); 
var iconBase = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+number+'|F2A327|000000'; 
/* var iconBase = 'http://www.google.com/mapfiles/marker'+number+'.png'; */ 
var shadow = new google.maps.MarkerImage('/wp-content/themes/digimix/shadow.png', new google.maps.Size(37, 34)); 


function initialize() { 
map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 4, 
    minZoom: 4, 
    maxZoom: 18, 
    center: new google.maps.LatLng(41.4758162, -71.29734339999999), 
    mapTypeControl: true, 
mapTypeControlOptions: { 
    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
}, 
    panControl: false, 
    scaleControl: false, 
    zoomControl: true, 
    zoomControlOptions: { 
    style: google.maps.ZoomControlStyle.LARGE 
}, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

//create empty LatLngBounds object 
var bounds = new google.maps.LatLngBounds(); 

//when infowindow is open, close info window when you click on the map 
google.maps.event.addListener(map, 'click', function() { 
    infowindow.close(); 
}); 

for (var i = 0; i < locations.length; i++) { 
    var marker = new google.maps.Marker({ 
     position: locations[i].latlng, 
     icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+(i+1)+'|F2A327|000000', 
     title: "Click for more information", 
     shadow: shadow, 
     map: map, 
    }); 


    //extend the bounds to include each marker's position 
    bounds.extend(marker.position); 


    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
     infowindow.setContent(locations[i].info); 
     infowindow.open(map, marker); 
     } 
    })(marker, i)); 
} 

    //now fit the map to the newly inclusive bounds 
map.fitBounds(bounds);} 

回答

0

您沒有google.maps.Marker對象的全局gmarkkers數組來觸發點擊事件。您需要在實例化標記時創建該標記。

// in global scope (outside of any function) 
var gmarkers = []; 

function initialize() { 
    map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 4, 
    minZoom: 4, 
    maxZoom: 18, 
    center: new google.maps.LatLng(41.4758162, -71.29734339999999), 
    mapTypeControl: true, 
    mapTypeControlOptions: { 
     style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
    }, 
    panControl: false, 
    scaleControl: false, 
    zoomControl: true, 
    zoomControlOptions: { 
     style: google.maps.ZoomControlStyle.LARGE 
    }, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    //create empty LatLngBounds object 
    var bounds = new google.maps.LatLngBounds(); 

    //when infowindow is open, close info window when you click on the map 
    google.maps.event.addListener(map, 'click', function() { 
    infowindow.close(); 
    }); 

    for (var i = 0; i < locations.length; i++) { 
    var marker = new google.maps.Marker({ 
     position: locations[i].latlng, 
     icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+(i+1)+'|F2A327|000000', 
     title: "Click for more information", 
     shadow: shadow, 
     map: map, 
    }); 

    gmarkers.push(marker); 

    //extend the bounds to include each marker's position 
    bounds.extend(marker.position); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
     infowindow.setContent(locations[i].info); 
     infowindow.open(map, marker); 
     } 
    })(marker, i)); 
    } 

    //now fit the map to the newly inclusive bounds 
    map.fitBounds(bounds); 
} 
+0

非常感謝提示@geocodezip!全部修好! – tinesays

相關問題