Google Maps API v3与自定义图标的兴趣点

时间:2012-06-29 21:31:01

标签: google-maps

我有一个页面拉着我所在地区的学校,教堂和公园,但我想用3个不同的图标设计3个POI。我搜索了Google甚至所有文档,但找不到答案。

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);
  });
}

1 个答案:

答案 0 :(得分:11)

请查看我的快速而肮脏的Demo。我们的想法是使用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);
    });
}