【问题标题】:Initializing Markers and Infowindow in Ionic Google Maps在 Ionic Google Maps 中初始化标记和信息窗口
【发布时间】:2018-04-02 19:36:32
【问题描述】:

我又一次遇到了我的克星,添加谷歌标记并在信息窗口中设置内容。在这段代码中,我已经完成了地理定位,我想从我当前的位置搜索附近的地方。我搜索的地方将从我已经实现的 Ionic 列表页面中检索。从我在列表中选择的内容中选择。消防站,我希望我的代码执行地点搜索,结果在我的地图上显示为标记,并带有来自谷歌图书馆的信息窗口内容。我的问题是标记不在我的地图上,您可能会猜到,这意味着没有信息窗口。如果你能给我一些方向。我正在 Ionic 中执行此操作。

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { DirectionPage} from '../direction/direction';

/**
 * Generated class for the PlacesPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

 declare var google;
 this.marker = [];

@Component({
  selector: 'page-places',
  templateUrl: 'places.html',
})
export class PlacesPage {

  places: Array<any>;
  map: any;
  currentLocation: any;
  latitude: Number;
  longitude: Number;
  keyword: String;
  searchType: String;
  distance: Number; 

 constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.latitude = navParams.get('latitude');
    this.longitude = navParams.get('longitude');
    this.keyword = navParams.get('keyword');
    this.searchType = navParams.get('type');
    this.distance = navParams.get('distance');
  }

  ionViewDidLoad() {
    this.queryPlaces().then((results : Array<any>)=>{
      for (let i=0; i < results.length; i++){
        this.createMarker(results[i]);
      }
      this.places = results;
    }, (status)=>console.log(status));
  }

  queryPlaces(){
    this.currentLocation = new google.maps.LatLng(this.latitude,this.longitude);
let mapOptions = {
  zoom: 10,
  center: this.currentLocation,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(document.getElementById("map"), mapOptions);

let service = new google.maps.places.PlacesService("service");

let request = {
  location : this.currentLocation,
  radius: this.distance,
  types: [this.searchType],
  rankBy: google.maps.places.DISTANCE      
};

return new Promise((resolve,reject)=>{
  service.nearbySearch(request, function(results,status){
    if (status == google.maps.places.PlacesServiceStatus.OK){
      resolve(results);
    }else{
      reject(results);
    }
  });
});    

 } 

 createMarker(place){
    let marker = new google.maps.Marker({
      map: this.map,
      animation: google.maps.Animation.DROP,
      position: place.geometry.location,
      title: place.name,
    });

google.maps.event.addListener(marker, 'click', function(){
  let infowindow = new google.maps.infowindow({
    content : this.place
    });
    infowindow.open(this.map,marker);    
  });
}

goToDirectionPage(index){

} 
}

【问题讨论】:

    标签: javascript google-maps ionic-framework google-maps-api-3 google-places


    【解决方案1】:

    我意识到 infowindow 中的“I”需要大写。此外,place.geometry.location 获取标记对象的位置。

    createMarker(place){
        let marker = new google.maps.Marker({
          map: this.map,
          animation: google.maps.Animation.DROP,
          position: place.geometry.location,
          title: place.name,
        });
    
    google.maps.event.addListener(marker, 'click', function(){
      let infowindow = new google.maps.InfoWindow({
        content: place.name 
      });
      infowindow.open(this.map,marker);
    })
    

    【讨论】:

    • 这段代码是您问题的答案吗?如果是这样,您应该添加一个简短的解释,说明它是如何工作的以及它为什么解决了这个问题。如果您尝试向问题添加更多代码,您应该编辑问题,而不是发布答案。
    • 还有。您可以使用 Ionic 4 Implementation 更新此答案。此实现现已弃用
    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 2012-06-06
    • 2014-02-18
    • 2014-05-14
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多