在Google Map上添加多个标记

时间:2018-08-14 09:26:27

标签: javascript angular ionic-framework

我正在构建一个Ionic应用程序,并试图在Google地图上添加多个标记。但是控制台日志显示map未定义。地图已加载,但标记不会显示。你能帮我吗?

  ionViewDidLoad() {
    this.addMarkers();
  }

  ionViewWillLoad() {
    this.location = this.navParams.get('location');
    this.cityName = this.navParams.get('city');
    if (this.location) {
      this.longtitude = this.location.longtitude;
      this.latitude = this.location.latitude;
      this.getCityInformation();
      this.loadMap();
    }

  loadMap() {
    let mapOptions: GoogleMapOptions = {
      camera: {
        target: {
          lat: this.latitude,
          lng: this.longtitude
        },
        zoom: 13,
        tilt: 0
      }
    };

    this.map = GoogleMaps.create('map_canvas', mapOptions);
  }

  addMarkers() {
    this.places.forEach(function (place) {
      let options: MarkerOptions =
      {
        title: place.name,
        icon: 'blue',
        animation: 'DROP',
        position: {
          lat: place.location.latitude,
          lng: place.location.longtitude
        }
      };
      this.map.addMarker(options);
    })
  }

3 个答案:

答案 0 :(得分:1)

googleMapOption = {
    myMap: function(selector) {
        var mapProp = {
            center: new google.maps.LatLng(47.91882274682187, 106.91763639450073),
            zoom: 15,
        };
        map = new google.maps.Map(document.getElementById(selector), mapProp);
        map.addListener('click', function(event) {
            googleMapOption.addMarker(event.latLng);               
        });

    },
    addMarker: function(location) {
        this.deleteMarkers();
        var marker = new google.maps.Marker({
            position: location,
            map: map
        });
        markers.push(marker);
    },
    setMapOnAll: function(map) {
        for (var i = 0; i < markers.length; i++) {
            markers[i].setMap(map);
        }
    },
    showMarkers: function() {
        this.setMapOnAll(map);
    },
    deleteMarkers: function() {
        this.setMapOnAll(null);
        markers = [];
    }
}

尝试这样的事情

用法googleMapOption.myMap('id of div');

答案 1 :(得分:1)

尝试下面的代码。

HTML

<ion-header>
  <ion-navbar>
    <ion-title>
      Location
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <div id="map_canvas"></div> 
</ion-content>

CSS

#map_canvas {
  height: 100%;
}

JS

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { GoogleMaps, GoogleMap, GoogleMapOptions, MarkerOptions, Marker } from '@ionic-native/google-maps';

declare var google;

export class SearchResultsPage extends MapPage {

  @ViewChild('map') mapElement: ElementRef;
  private map: any;

  constructor(public navCtrl: NavController,  public navParams: NavParams, private googleMaps: GoogleMaps) {}


  ionViewDidLoad() {
    this.initMap();
    setTimeout(function() {
      this.addMarkers();
    }, 1000);
  }

  initMap() {
      let mapOptions: GoogleMapOptions = {
        camera: {
          target: {
            lat: this.latitude,
            lng: this.longtitude
          },
          zoom: 13,
          tilt: 0
        }
      };

      this.map = GoogleMaps.create('map_canvas', mapOptions);

    },

    addMarkers() {
      this.places.forEach(function(place) {
        let options: MarkerOptions = {
          title: place.name,
          icon: 'blue',
          animation: 'DROP',
          position: {
            lat: place.location.latitude,
            lng: place.location.longtitude
          }
        };

        this.map.addMarker(options);

      });
    }
}

答案 2 :(得分:0)

问题出在函数作用域上。这是更新的代码:

  addMarkers() {
    let map = this.map;
    this.places.forEach(function (place) {
      let options: MarkerOptions =
      {
        title: place.name,
        icon: 'blue',
        animation: 'DROP',
        position: {
          lat: place.location.latitude,
          lng: place.location.longtitude
        }
      };
      map.addMarkerSync(options);
    })
  }
相关问题