自定义Google地图标记的图标

时间:2015-05-09 10:32:45

标签: javascript jquery google-maps google-maps-api-3

我在第28行尝试更改一组标记的图标时出现'未捕获的语法错误:意外的标识符'。标记是循环的一部分,但我不明白为什么它应该是一个问题。

提前致谢。

var map;
function initialize() {
    /*var myLatlng = new google.maps.LatLng(51.516079, -0.107781);*/
    var mapOptions = {
        zoom: 13,
        /*center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP*/
}
   map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


var locations = [
      ['Fabric', 51.5195422,-0.10280890000001364, 4],
      ['Somerset House', 51.51073239999999,-0.11693769999999404, 5],
      ['Museum of London', 51.5178767,-0.09603170000002592, 3],
      ['St Pauls Cathedral', 51.5142737,-0.09899240000004284, 2],
      ['Grays Inn Gardens', 51.5193096,-0.1123606999999538, 1]
    ];

/*var image = new google.maps.MarkerImage('https://www.bhf.org.uk/~/media/images/admin/logo/bhf-logo.png')*/

  var marker2, i;

  for (i = 0; i < locations.length; i++) {  
        marker2 = new google.maps.Marker({
        position : new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
        icon: 'https://www.bhf.org.uk/~/media/images/admin/logo/bhf-logo.png',
        });
      }

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);


        //=====Initialise InfoWindow
    var infowindow = new google.maps.InfoWindow({
      map: map,
      position: pos,
      content: 'Your location'
  });

 map.setCenter(pos);
  },

  function(){
      handleNoGeolocation(true);
    });
  } 

  else {handleNoGeolocation(false);
    }
  }


  function handleNoGeolocation(errorflag) {
    if(errorflag) {
      var content = 'Error: The Geolocation service failed.';
    } else { 
      var content = 'Error: Your browser doesn\'t support geolocation.';
    }  

    var options = {
    map: map,
    position: new google.maps.LatLng(51.511737, -0.105064),
    content: content
    };

    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
  }


/*
   //=====Eventlistener for InfoWindow
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
    map.setZoom(12);
  })

//========Eventlistener for InfoWindow Zoom out
 google.maps.event.addListener(marker, 'dblclick', function() {
    map.setZoom(8);
    infowindow.close(map,marker);
  });

//======Recenter
google.maps.event.addListener(map,'center_changed',function() {
  window.setTimeout(function() {
    map.panTo(marker.getPosition());
  },10000);
});
*/

google.maps.event.addDomListener(window, 'load', initialize)

1 个答案:

答案 0 :(得分:0)

您在map: map后错过了逗号:

or (i = 0; i < locations.length; i++) {  
    marker2 = new google.maps.Marker({
    position : new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
    icon: 'https://www.bhf.org.uk/~/media/images/admin/logo/bhf-logo.png',
    });
  }