在Google Maps V3中使用图标字体作为标记

gfa*_*faw 49 google-maps-api-3 google-maps-markers font-awesome icon-fonts

我想知道是否可以使用图标字体图标(例如Font Awesome)作为Google Maps V3中的标记来替换默认标记.要在HTML或PHP文档中显示/插入它们,标记的代码将是:

<i class="icon-map-marker"></i>
Run Code Online (Sandbox Code Playgroud)

nat*_*n-m 81

我只是遇到了同样的问题 - 决定做一个快速而又脏的转换并在github上托管.

https://github.com/nathan-muir/fontawesome-markers

您可以手动包含JS文件,或使用npm install fontawesome-markers或bower install fontawesome-markers.

只需包含javascript文件fontawesome-markers.min.js,您可以像这样使用它们:

new google.maps.Marker({
    map: map,
    icon: {
        path: fontawesome.markers.EXCLAMATION,
        scale: 0.5,
        strokeWeight: 0.2,
        strokeColor: 'black',
        strokeOpacity: 1,
        fillColor: '#f8ae5f',
        fillOpacity: 0.7
    },
    clickable: false,
    position: new google.maps.LatLng(lat, lng)
});
Run Code Online (Sandbox Code Playgroud)

编辑(2016年4月):现在有v4.2的包 - > v4.6.1

  • 我认为值得一提的是js文件大0.5 MB. (4认同)
  • +1用于保持脚本在一年后更新! (2认同)

gui*_*ido 39

我知道这是一个旧帖子,但万一你MarkerLabel现在可以使用该对象:

var marker = new google.maps.Marker({
    position: location,
    map: map,
    label: {
        fontFamily: 'Fontawesome',
        text: '\uf299'
    }
});
Run Code Online (Sandbox Code Playgroud)

为我工作.

请参阅谷歌地图标记上的fontawesome图标.

参考Google地图制作工具

  • 这是合法的!我不想要粉红色的标记,所以我只是设置`Marker.icon =''`一个空格,就像Jason的回答一样.[MarkerLabel](https://developers.google.com/maps/documentation/javascript/reference#MarkerLabel)可以采用各种样式,包括fontSize和color来自定义图标.很简单.谢谢@guido! (5认同)
  • 对于更高版本的API,如果上面的代码不起作用,只需将“ Fontawesome”用双引号“'Fontawesome'”括起来/sf/ask/3517982261/ #51788072 (2认同)

Jas*_*son 30

这是我尝试同样的事情(使用"markerwithlabel"实用程序库)之前我才意识到Nathan在上面做得更优雅:http://jsfiddle.net/f3xchecf/

function initialize() {

    var myLatLng = new google.maps.LatLng( 50, 50 ),
        myOptions = {
            zoom: 4,
            center: myLatLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            },

        map = new google.maps.Map( document.getElementById( 'map-canvas' ), myOptions ),

     marker = new MarkerWithLabel({
       position: myLatLng,
       draggable: true,
       raiseOnDrag: true,
       icon: ' ',
       map: map,
         labelContent: '<i class="fa fa-send fa-3x" style="color:rgba(153,102,102,0.8);"></i>',
       labelAnchor: new google.maps.Point(22, 50)
     });

    marker.setMap( map );
}

initialize();
Run Code Online (Sandbox Code Playgroud)


Jer*_*nch 15

重量轻的解决方案

  • fontawesome标志物: 480kb
  • markerwithlabel: 25kb

要避免这些依赖关系,只需转到fontawesome-markers,找到所需图标的路径,并按如下方式包含它:

var icon = {
    path: "M27.648-41.399q0-3.816-2.7-6.516t-6.516-2.7-6.516 2.7-2.7 6.516 2.7 6.516 6.516 2.7 6.516-2.7 2.7-6.516zm9.216 0q0 3.924-1.188 6.444l-13.104 27.864q-.576 1.188-1.71 1.872t-2.43.684-2.43-.684-1.674-1.872l-13.14-27.864q-1.188-2.52-1.188-6.444 0-7.632 5.4-13.032t13.032-5.4 13.032 5.4 5.4 13.032z",
    fillColor: '#E32831',
    fillOpacity: 1,
    strokeWeight: 0,
    scale: 0.65
}

marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    icon: icon
});
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 13

在现代浏览器中,可以使用画布将字体呈现为png,然后使用数据URI方案:


  function getIcon(glyph, color) {
    var canvas, ctx;
    canvas = document.createElement('canvas');
    canvas.width = canvas.height = 20;
    ctx = canvas.getContext('2d');
    if (color) {
      ctx.strokeStyle = color;
    }
    ctx.font = '20px FontAwesome';
    ctx.fillText(glyph, 0, 16);
    return canvas.toDataURL();
  }
Run Code Online (Sandbox Code Playgroud)

例如:getIcon("\uf001")对于音符.

  • `ctx.fillStyle = color;`,或`ctx.strokeText`而不是`ctx.fillText` (3认同)

Mat*_* P. 7

如果您想要带有 awesomefont ICON INSIDE 的 awesomefont MARKER:

1.复制awesomefont 标记的SVG 路径(点击下载并复制SVG 路径)并将其用作icon(记得注明作者,请参阅许可)。然后您可以将其颜色更改为您想要的任何颜色。

2.因为label你只插入了真棒字体图标代码和你想要的颜色。

html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<div id="map"></div>
<script>
  function init() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 6,
      center: new google.maps.LatLng(51.509865, -0.118092)
    });
    var icon = {
        path: "M172.268 501.67C26.97 291.031 0 269.413 0 192 0 85.961 85.961 0 192 0s192 85.961 192 192c0 77.413-26.97 99.031-172.268 309.67-9.535 13.774-29.93 13.773-39.464 0z", //SVG path of awesomefont marker
        fillColor: '#333333', //color of the marker
        fillOpacity: 1,
        strokeWeight: 0,
        scale: 0.09, //size of the marker, careful! this scale also affects anchor and labelOrigin
        anchor: new google.maps.Point(200,510), //position of the icon, careful! this is affected by scale
        labelOrigin: new google.maps.Point(205,190) //position of the label, careful! this is affected by scale
    }

    var marker = new google.maps.Marker({
      position: map.getCenter(),
      map: map,
      icon: icon,
      label: {
        fontFamily: "'Font Awesome 5 Free'",
        text: '\uf0f9', //icon code
        fontWeight: '900', //careful! some icons in FA5 only exist for specific font weights
        color: '#FFFFFF', //color of the text inside marker
      },
    });
  }
  google.maps.event.addDomListener(window, 'load', init);
</script>
Run Code Online (Sandbox Code Playgroud)


Jus*_*elt 5

可以直接从@fortawesome/free-solid-svg-icons导入 svgs并使用Icon Interface。

import { faMapMarkerAlt } from "@fortawesome/free-solid-svg-icons";

function initMap(): void {
  const center = { lat: 0, lng: 0 };

  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 9,
      center,
    }
  );

  new google.maps.Marker({
    position: { lat: 0, lng: 0 },
    map,
    icon: {
      path: faMapMarkerAlt.icon[4] as string,
      fillColor: "#0000ff",
      fillOpacity: 1,
      anchor: new google.maps.Point(
        faMapMarkerAlt.icon[0] / 2, // width
        faMapMarkerAlt.icon[1] // height
      ),
      strokeWeight: 1,
      strokeColor: "#ffffff",
      scale: 0.075,
    },
  });
}
Run Code Online (Sandbox Code Playgroud)

可以尝试一下: https: //codesandbox.io/embed/github/googlemaps/js-samples/tree/sample-marker-modern