Google地图中的离子2动态标记,带有个人资料图片

时间:2017-03-04 16:08:39

标签: javascript google-maps angular ionic2

我正在使用存储在数据库中的跟踪数据构建ionic2应用程序,我能够获取地理位置数据并初始化地图,但我有一个markers数组profileimageurl和{{1 }} lat值,我试图将profileimageurl作为图标带到地图上的标记列表,但它不起作用

Google地图初始

lng

标记数组

initMap(): Promise<void> {
 let promise: Promise<void> = new Promise<void>((resolve, reject) => {
  Geolocation.getCurrentPosition().then((position) => {
    let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    let GooleMap = new google.maps.Map(document.getElementById('map'), {
      center: latLng,
      zoom: 18
    });
    let marker = new google.maps.Marker({
      position: latLng,
      map: GooleMap,
      title: 'My Location',
    });
  });
 });
 return promise;
}

我期待这样的结果 enter image description here

我一直在努力解决这个问题,我非常感谢你的回答,谢谢

1 个答案:

答案 0 :(得分:3)

我会覆盖google.maps.OverlayView来实现它。

class CustomMarker extends google.maps.OverlayView {
  marker: any;
  clickListener: google.maps.MapsEventListener;

  constructor(private latlng, map, private args) {
    super();
    this.setMap(map);
  }

  draw() {
    const panes = this.getPanes();
    let marker = this.marker;

    if (!marker) {
      marker = this.marker = document.createElement('div');
      marker.className = 'marker';

      let img = document.createElement('img');
      img.src = this.args.img;
      marker.appendChild(img);

      let point = this.getProjection().fromLatLngToDivPixel(this.latlng);
      if (point) {
        marker.style.left = (point.x - 50) + 'px';
        marker.style.top = (point.y - 50) + 'px';
      }

      this.clickListener = google.maps.event.addDomListener(marker, "click", (event) => {
        alert('You clicked on a custom marker!');
        google.maps.event.trigger(this, "click");
      });

      panes.overlayImage.appendChild(marker);
    }
  }

  remove() {
    if (this.marker) {
      this.marker.parentNode.removeChild(this.marker);
      this.clickListener.remove();
      this.marker = null;
    }
  }

  getPosition() {
    return this.latlng;
  }
}

<强> Plunker Example

P.S。我猜你已经安装了@types/google-maps