2015-12-02 111 views
2

我已经实现了谷歌标记,并且我想在标记内显示一个名称。我试图改变标签:如下面的代码,但它只显示第一个字符..可以有人告诉我如何解决这个问题?在谷歌地图标记中显示多个字符

这里是从谷歌的代码标记

function initialize() { 
    var bangalore = { lat: 12.97, lng: 77.59 }; 
    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 12, 
    center: bangalore 
    }); 

    // This event listener calls addMarker() when the map is clicked. 
    google.maps.event.addListener(map, 'click', function(event) { 
    addMarker(event.latLng, map); 
    }); 

    // Add a marker at the center of the map. 
    addMarker(bangalore, map); 
} 

// Adds a marker to the map. 
function addMarker(location, map) { 
    // Add the marker at the clicked location, and add the next-available label 
    // from the array of alphabetical characters. 
    var marker = new google.maps.Marker({ 
    position: location, 
    label: "lets showthis", 
    map: map 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

Link to the google maps

+0

可能重复http://stackoverflow.com/questions/32467212/google-maps-marker-label-with-multiple-characters) – geocodezip

回答

0

google.maps.Markerlabel是一个单独的字符。

documentation明确指出这样的:

MarkerLabel对象规范

google.maps.MarkerLabel对象规范

这些选项指定的标记标签的外观。 标记标签是将出现在标记内的文本的单个字符。如果您使用自定义标记,可以使用Icon类中的labelOrigin属性重新定位它。

text |类型:字符串 要在标签中显示的文本。 只显示该字符串的第一个字符。

如果你想要一个以上的字符(至少现在,有一个open feature request to make that number larger),您需要使用第三方库像MarkerWithLabel

example fiddle

代码片断:

function initialize() { 
 
    var bangalore = { 
 
    lat: 12.97, 
 
    lng: 77.59 
 
    }; 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 12, 
 
    center: bangalore 
 
    }); 
 

 
    // This event listener calls addMarker() when the map is clicked. 
 
    google.maps.event.addListener(map, 'click', function(event) { 
 
    addMarker(event.latLng, map); 
 
    }); 
 

 
    // Add a marker at the center of the map. 
 
    addMarker(bangalore, map); 
 
} 
 

 
// Adds a marker to the map. 
 
function addMarker(location, map) { 
 
    // Add the marker at the clicked location, and add the next-available label 
 
    // from the array of alphabetical characters. 
 
    var marker = new MarkerWithLabel({ 
 
    position: location, 
 
    labelContent: "lets showthis", 
 
    map: map, 
 
    labelAnchor: new google.maps.Point(35, 120), 
 
    labelClass: "labels", // the CSS class for the label 
 
    labelInBackground: false, 
 
    icon: pinSymbol('red') 
 
    }); 
 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize); 
 

 
// creates an SVG marker in the teardrop shape of the "normal" marker 
 
function pinSymbol(color) { 
 
    return { 
 
    path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z', 
 
    fillColor: color, 
 
    fillOpacity: 1, 
 
    strokeColor: '#000', 
 
    strokeWeight: 2, 
 
    scale: 4 
 
    }; 
 
}
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
} 
 
.labels { 
 
    color: black; 
 
    background-color: red; 
 
    font-family: "Lucida Grande", "Arial", sans-serif; 
 
    font-size: 10px; 
 
    text-align: center; 
 
    width: 70px; 
 
    white-space: nowrap; 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js"></script> 
 
<div id="map"></div>

[与多个字符谷歌地图标识标签(的
+0

感谢您的回答。这工作..但我认为这将是更好的,如果文本嵌入到标记,而不是通过CSS和指针的位置...所以它会根据文本大小调整大小。有什么办法来实现这一点? – Abhilash

+0

但是当我试图通过marker.setMap(null)删除标记时,我收到错误,this.label不支持setMap方法。有没有其他方法可以从地图中删除这个标记? –

+0

这是另一个问题。但是您可能会寻找[MarkerWithLabel]的更新版本(http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/),[在我的回答中切换小提琴中的标记作品](http://jsfiddle.net/wjv4f7pk/3/) – geocodezip