8

我在我给定区域的学校、教堂和公园中有一个页面,但我想用 3 个不同的图标设置 3 个 POI 的样式。我搜索了谷歌甚至所有文档,但找不到答案。

var map;
var infowindow;

function initialize() {

  // Center of Map
  var centerLatlng = new google.maps.LatLng(29.745376, -95.380125);

  // Marker Icons Declaration
  var icon = new google.maps.MarkerImage("smLinks-twitter.png", null, null, new google.maps.Point(41,47));

  // Map Options
  var myOptions = {
    zoom: 16,
    center: centerLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    icons: icon 
  };

  // Draw the map
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Marker Icons Implementation
  markers = new google.maps.Marker({ 
    position: centerLatlng,
    map: map, 
    title: 'Center of Map', 
    icon: icon 
  });

  // Services: Places
  var request = {
    location: centerLatlng,
    radius: 800,
    types: ["school", "church", "park"]
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.search(request, callback);

} // function initialize()

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location,
    icon: icon
  });
  google.maps.event.addListener(marker, 'mouseover', function() {
    infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
    infowindow.open(map, this);
  });
}
4

1 回答 1

11

请看我的快速而肮脏的演示。这个想法是使用place.types数组来确定您要添加到地图中的地点类型。我简单地为这个数组的第一项(通常是 2 或 3 项长)分配了一个标记,可能类似于:

["school", "establishment"]

在某些情况下,“大学”出现在“学校”之前,因此使用了“大学”图标。您会想改进将类型与图标匹配的方式,即优先考虑学校或大学?也许遍历数组以寻找正确的类型。一个地方 (general_contractor) 不在我的图标列表中,因此默认的 pin 标记放置在那里。如果您检查iconType实际上是否具有所需的类型,则可以使用“默认”图标。我会把这些细节留给你=)

这是我用于图标的来源:https ://sites.google.com/site/gmapsdevelopment/

function createMarker(place) {
    var placeLoc = place.geometry.location;

    var iconType = {};
    iconType['school'] = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
    iconType['church'] = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
    iconType['park'] = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
    iconType['university'] = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";

    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        icon: iconType[place.types[0]]
    });

    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
        infowindow.open(map, this);
    });
}

或者,使用 switch 语句:

function createMarker(place) {
    var placeLoc = place.geometry.location;

    var iconUrl;
    switch (place.types[0]) {
    case 'school':
        iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
        break;
    case 'church':
        iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
        break;
    case 'park':
        iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
        break;
    case 'university':
        iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";
        break;
    default:
        iconUrl = "http://maps.google.com/mapfiles/kml/pal4/icon39.png";
    }

    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        icon: iconUrl
    });

    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
        infowindow.open(map, this);
    });
}
于 2012-06-29T22:50:55.380 回答