2017-11-17 10 views
-1

다른 StackOverflow post의 도움으로 마커를 클릭 가능한 링크로 만드는지도가 있습니다.Google지도에서 마커 레이블 지정

사용자가 링크 할 위치를 알 수 있도록 마커에 종류가 있어야합니다 (지금은 빈 핀 일뿐입니다).하지만이 코드를 수정하여 빌드하는 방법을 모르겠습니다. 에서 그 함수.

function initMap() { 

var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 7, 
     center: {lat: 53.618939, lng: -2.1602328} 
    }); 

var locations = [ 
["http://www.google.com", {lat: 53.618939, lng: -2.1601132}], 
["http://www.google.com", {lat: 53.61953, lng: -2.1623746}], 
["http://www.google.com", {lat: 53.6178993, lng: -2.1601596}], 
["http://www.google.com", {lat: 53.5369545, lng: -1.9941523}], 
["http://www.google.com", {lat: 53.5380496, lng: -1.9974085}],]; 

var markers = locations.map(function(location) { 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: location[1], 
     url: location[0] //Will be different for each marker 
    }); 

    google.maps.event.addListener(marker, 'click', function(){ 
     window.location.href = this.url; 
    }); 
    return marker; 
}); 

// Add a marker clusterer to manage the markers. 
var markerCluster = new MarkerClusterer(map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'}); 
} 
+0

당신은 문자 나 숫자로 고유하게 표시된 각 마커를 원하는 뜻? – geocodezip

+0

[문서의이 예] (https://developers.google.com/maps/documentation/javascript/examples/marker-labels)와 비슷합니까? – geocodezip

답변

0

는 (적어도 최대 26 마커)의 고유 문자로 각 마커 레이블을하려면, 당신이 따를 수 this example in the documentation

var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
var labelIndex = 0; 

var markers = locations.map(function(location) { 
    var marker = new google.maps.Marker({ 
    map: map, 
    position: location[1], 
    label: labels[labelIndex++ % labels.length], 
    url: location[0] //Will be different for each marker 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
    window.location.href = this.url; 
    }); 
    return marker; 
}); 

proof of concept fiddle

screen shot of resulting map

코드 :

// In the following example, markers appear when the user clicks on the map. 
 
// Each marker is labeled with a single alphabetical character. 
 
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
 
var labelIndex = 0; 
 

 
function initMap() { 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 7, 
 
    center: { 
 
     lat: 53.618939, 
 
     lng: -2.1602328 
 
    } 
 
    }); 
 

 
    var locations = [ 
 
    ["http://www.google.com", {lat: 53.618939,lng: -2.1601132}], 
 
    ["http://www.google.com", {lat: 53.61953,lng: -2.1623746}], 
 
    ["http://www.google.com", {lat: 53.6178993,lng: -2.1601596}], 
 
    ["http://www.google.com", {lat: 53.5369545,lng: -1.9941523}], 
 
    ["http://www.google.com", {lat: 53.5380496,lng: -1.9974085}], 
 
    ]; 
 

 

 
    var markers = locations.map(function(location) { 
 
    var marker = new google.maps.Marker({ 
 
     map: map, 
 
     position: location[1], 
 
     label: labels[labelIndex++ % labels.length], 
 
     url: location[0] //Will be different for each marker 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'click', function() { 
 
     window.location.href = this.url; 
 
    }); 
 
    return marker; 
 
    }); 
 

 
    // Add a marker clusterer to manage the markers. 
 
    var markerCluster = new MarkerClusterer(map, markers, { 
 
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' 
 
    }); 
 
} 
 
google.maps.event.addDomListener(window, "load", initMap);
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/src/markerclusterer.js"></script> 
 
<div id="map"></div>

+0

Geocodezip에게 감사드립니다. 문서에서 라벨을 지정하는 예를 보았지만 A, B, C 등보다 자세한 설명이 필요했습니다. 또는 모든 마커가 알려진 위치 또는 업체가 될 수 있으므로 Google이 이미 해당 위치에 대해 가지고있는 데이터를 일반적으로 Google지도에서와 같이 표시됩니다. 죄송합니다 - 원래 질문이 변경되었습니다! – AndyClegg

+0

레이블에 무엇을 원하십니까? 입력 데이터에서 오는 것입니까? 어느 분야? (명백한 것이 없다 ...) – geocodezip

+0

그것은 어떤 식 으로든 참조 될 수 있다고 생각하는 데이터 = 문자열입니다. 아마도 나는 거기에서 빨판을 쥐고있을 것입니다 ... 나는 URL 옆에있는 코드에 설명을 추가하고 마커가 링크되는 곳을 표시 할 long/lat 정보 (예 : "Google Homepage"). https://code.google.com/archive/p/google-maps-utility-library-v3/wikis/Libraries.wiki#MarkerWithLabel '라벨이있는 마커'는 완벽 할 수 있지만 링크가 죽어 있으므로 할 수 있습니다. 어떻게 작동했는지에 대한 정보는 보이지 않습니다. 클러스터 맵에 이와 같은 것을 구현할 수 있습니까? – AndyClegg

관련 문제