繁体   English   中英

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

[英]ionic 2 dynamic markers in Google Maps with profile picture

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

谷歌地图初始化

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;
}

标记数组

  markers: marker[] = [
  {
      lat: 51.673858,
      lng: 7.815982,
      label: 'A',
      profileImage: "http://www.gravatar.com/avatar/d735414fa8687e8874783702f6c96fa6?s=90&d=identicon&r=PG"
  },
  {
      lat: 51.373858,
      lng: 7.215982,
      label: 'B',
      profileImage: "http://placekitten.com/90/90"
  }
]

我期待这样的结果 在此处输入图片说明

我已经为此苦苦挣扎了一段时间,非常感谢您的答复,谢谢

我会重写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;
  }
}

柱塞示例

PS我猜你已经安装了@types/google-maps

暂无
暂无

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

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