2

Google의 새로운 장소 라이브러리를 사용하여 여러 유형의 장소 검색 결과에 다른 맞춤 아이콘을 사용하는 논리를 알아낼 수 없습니다. 지도. 예를 들어 -이 요청합니다 경기장 및 공원Google 지역 정보가 포함 된 Google지도에서 다른 장소 유형별 이미지가 다릅니다.

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

가 생성 마커 기능이

var emu = 'EMU_test_icon.png' 

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

처럼 보이는 만들 마커 기능 그래서 방법을 알고를 포함 모든 마커에 EMU_test_icon 이미지를 사용하도록 지정하고 있지만 "경기장"장소 유형의 결과를 나열한 아이콘 (에뮤)으로 보내려면 어떻게해야합니까? 다른 맞춤 설정 아이콘 (image2)에 'park'결과에 대한 아이콘이 있습니까?

답변

0

createMarker 메서드에 전달되는 "place"매개 변수에는 도움이되는 "type"속성이 있습니다. PlaceSearchResults 제목 아래의 here 참조를 참조하십시오.

지도에 두 아이콘을 저장하고 장소 유형에 따라 아이콘 유형을 조회 할 수 있습니다.

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

} 

* 참고 : var에 다른 곳에서 선언 "지도"를 가정 할

이 9

* 주 2 (배열 프로토 타입에 같이 IndexOf 기능을 추가하지 않고) IE <에서 작동하지 않습니다