最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Map marker url - Stack Overflow

programmeradmin1浏览0评论

Can anybody help. I have below code but when I click on the marker it redirects me to the same page:

<script src="" type="text/javascript"></script>

<script type="text/javascript"> 
function initialize() {
  var myOptions = {
    zoom: 14,
    center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: true,
    scrollwheel: false
  }

  var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

  for (var i = 0; i < locations.length; i++) {
      var image = new google.maps.MarkerImage('images/greenmarkers/'+ i +'.png');
      var location = locations[i];
      var myLatLng = new google.maps.LatLng(location[1], location[2]);
      var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image,
          title: location[0],
          url: location[3]
      });


google.maps.event.addListener(marker, 'click', function() {
  window.location.href = marker.url;
});
  }
}
</script>

<script type="text/javascript"> 
var locations = [['test', 63.3740200000000, 19.6301320000000, ''],['test1', 63.3725155758185, 19.6264879703522, ''],['test2', 63.3762550022764, 19.6305649280548, '']];
</script> 

Can anybody help. I have below code but when I click on the marker it redirects me to the same page:

<script src="http://maps.google./maps/api/js?sensor=false" type="text/javascript"></script>

<script type="text/javascript"> 
function initialize() {
  var myOptions = {
    zoom: 14,
    center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: true,
    scrollwheel: false
  }

  var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

  for (var i = 0; i < locations.length; i++) {
      var image = new google.maps.MarkerImage('images/greenmarkers/'+ i +'.png');
      var location = locations[i];
      var myLatLng = new google.maps.LatLng(location[1], location[2]);
      var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image,
          title: location[0],
          url: location[3]
      });


google.maps.event.addListener(marker, 'click', function() {
  window.location.href = marker.url;
});
  }
}
</script>

<script type="text/javascript"> 
var locations = [['test', 63.3740200000000, 19.6301320000000, 'http://www.test.'],['test1', 63.3725155758185, 19.6264879703522, 'http://www.test1.'],['test2', 63.3762550022764, 19.6305649280548, 'http://www.test2.']];
</script> 
Share Improve this question edited Sep 9, 2013 at 8:16 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Aug 12, 2011 at 15:35 StipeStipe 575 silver badges9 bronze badges 2
  • Looks like the mon function-in-a-loop issue. See stackoverflow./questions/6939983/… for the basic way to fix it. – Matt Ball Commented Aug 12, 2011 at 15:57
  • Thanks Matt. I tried but no luck. – Stipe Commented Aug 12, 2011 at 17:28
Add a ment  | 

2 Answers 2

Reset to default 3

Here is a working code:

function loadURL(marker) {
    return function () {
        window.location.href = marker.url;
    }
}

function initialize() {
  var myOptions = {
    zoom: 5,
    center: new google.maps.LatLng(50.00, 50.00),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: true,
    scrollwheel: false
  }

  locations=[["aaa",51.00, 51.00, "http://www.atoztoa."], ["bbb",52.00, 52.00, "http://www.google."]];

  var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

  for (var i = 0; i < locations.length; i++) {
      var image = new google.maps.MarkerImage('roundedbutton1.png');
      var location = locations[i];
      var myLatLng = new google.maps.LatLng(location[1], location[2]);
      var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image,
          title: location[0],
          url: location[3]
      });

        google.maps.event.addListener(marker, 'click', loadURL(marker));
  }
}
marker[i] = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image,
          title: location[0],
          url: location[3]
      });


google.maps.event.addListener(marker[i], 'click', function() {
  window.location.href = this.url;
});
发布评论

评论列表(0)

  1. 暂无评论