简体   繁体   English

如何将字体真棒图标类添加到谷歌地图标签

[英]How to add font awesome icon class to google maps labels

I have spent hours looking for a way to add font awesome icon class to google maps label class but i cant find the solution.我花了几个小时寻找一种将字体真棒图标类添加到谷歌地图标签类的方法,但我找不到解决方案。 I have tried gmaps and markerwithlabes plugin but still i can't get it to work.我已经尝试过 gmaps 和 markerwithlabes 插件,但我仍然无法让它工作。 I tried overlays too but they get duplicated while zooming.. Here is a piece of my javascript code我也尝试过叠加,但它们在缩放时会重复。这是我的一段 javascript 代码

 var marker = new google.maps.Marker({ 'position': latLng, 'icon': markerImage , 'label': {'text': '<i></i>', 'color': "black"}, 'optimized': false });

I need help with this.我需要这方面的帮助。

I doubt you could actually insert html into the label.我怀疑您实际上是否可以将 html 插入标签中。 However, it is possible to use iconic fonts like FontAwesome.但是,可以使用像 FontAwesome 这样的标志性字体。

When you use an element and style it as a FA icon:当您使用元素并将其样式设置为 FA 图标时:

<i class="fa-camera-retro"></i>

FontAwesome inserts an unicode character to the :before pseudoselector of the i element: FontAwesome 在i元素的:before伪选择器中插入一个 unicode 字符:

.fa-camera-retro:before {
    content: "\f083";
}

So, to achieve the same effect in a marker label, you could declare that same unicode character as the text for the marker label:因此,要在标记标签中实现相同的效果,您可以声明与标记标签文本相同的 unicode 字符:

label:{
   text: String.fromCharCode("0xf083"),
   fontFamily: 'FontAwesome'
}

Of course this isn't as intuitive as using FA classnames, but then, again, nobody said it would be straightforward.当然,这不像使用 FA 类名那样直观,但是,再一次,没有人说这会很简单。

I left you a snippet for you to try:我给你留下了一个片段供你尝试:

 var centerMap={lat:0,lng:-77 }, zoom=7; function initMap(){ var map = new google.maps.Map(document.getElementById('map'), { center: centerMap, scrollwheel: false, zoom: zoom }); var marker=new google.maps.Marker({ position:map.getCenter(), map:map, label:{ text: String.fromCharCode("0xf083"), fontFamily: 'FontAwesome' } }); } document.addEventListener("DOMContentLoaded", function() { initMap(); });
 #map { width:100%; height:90vh; }
 <link href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <i class="fa fa-camera-retro"></i> fa-camera-retro <div id="map"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM