Javascript 使用Google Places library在Google Maps中为不同的地点类型提供不同的图像

Javascript 使用Google Places library在Google Maps中为不同的地点类型提供不同的图像,javascript,google-maps,google-maps-markers,google-places,Javascript,Google Maps,Google Maps Markers,Google Places,我是javascript新手,无法理解使用谷歌地图中的places库为不同类型地点的结果使用不同自定义图标的逻辑。 例如,这将要求体育场和公园 var request = { location: myLatLng, radius: 20000, types: ['stadium','park'] }; infowindow = new google.maps.InfoWindow(); var service = new google.

我是javascript新手,无法理解使用谷歌地图中的places库为不同类型地点的结果使用不同自定义图标的逻辑。 例如,这将要求体育场和公园

var request = {
      location: myLatLng,
      radius: 20000,
      types: ['stadium','park']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
回调函数包括创建标记函数

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

因此,我知道如何分配所有标记以使用EMU_test_图标图像,但如何将体育场场地类型的结果定向到我列出的EMU图标,但将停车场结果标记创建到不同的自定义图标图像2?

传递到createMarker方法中的place参数具有一个类型属性,可以提供帮助。请参阅PlaceSearchResults标题下的参考

您可以将这两个图标存储在地图中,并根据位置类型查找图标类型

// store the icons in a map, key'd by type
var iconMap = {'stadium': 'EMU_test_stadium_icon.png', 'park': 'EMU_test_park_icon.png'};

// access types array from PlaceResult object
//  search to find the string "stadium" and "park"
//  default the type to stadium and check to see if place is a park
//  if its a park update the type of icon we select
//  in the original call to create a marker, get the icon string by type
function createMarker(place) {

    var placeLoc = place.geometry.location,
        isStadium = place.types.indexOf("stadium") !== -1,
        isPark = place.types.indexOf("park") !== -1,
        iconType = "stadium";

    if (isPark) {
        iconType = "park";
    }

    new google.maps.Marker({
        map: map,
        icon: iconMap[iconType],
        position: place.geometry.location
    });

}  
*注意:如果不向阵列原型添加indexOf函数,这在IE<9中不起作用


*注2:假设您的map var在别处声明

传递到createMarker方法中的place参数有一个type属性可以提供帮助。请参阅PlaceSearchResults标题下的参考

您可以将这两个图标存储在地图中,并根据位置类型查找图标类型

// store the icons in a map, key'd by type
var iconMap = {'stadium': 'EMU_test_stadium_icon.png', 'park': 'EMU_test_park_icon.png'};

// access types array from PlaceResult object
//  search to find the string "stadium" and "park"
//  default the type to stadium and check to see if place is a park
//  if its a park update the type of icon we select
//  in the original call to create a marker, get the icon string by type
function createMarker(place) {

    var placeLoc = place.geometry.location,
        isStadium = place.types.indexOf("stadium") !== -1,
        isPark = place.types.indexOf("park") !== -1,
        iconType = "stadium";

    if (isPark) {
        iconType = "park";
    }

    new google.maps.Marker({
        map: map,
        icon: iconMap[iconType],
        position: place.geometry.location
    });

}  
*注意:如果不向阵列原型添加indexOf函数,这在IE<9中不起作用

*注2:假设您的映射变量在别处声明