1

私は数時間ウェブを検索していますが、私の一見、とても単純な問題には問題がないようです。複数のカスタムマーカーをIonic 2 + Googleマップと統合する

アプリを使用すると、iconのプロパティがgoogle.maps.Markerのように表示されない場合があります。

つまり、Ionic 2はカスタムマーカーのアイコン画像を定義できるGoogle Maps Javascript APIで何を使用していますか?

ここで関連コードをすべて提供しますが、このような質問にはあまり役立たないと感じています。 私はIonic 2について知っているので、Googleマップ、オンライン/オフライン状態、およびいくつかのデフォルトマーカーをアプリケーションのページに統合できました。

私のテスト画像ファイルは、google-maps.tsと同じフォルダにあります(これは現在何が起こっているかを把握するためのものです)。

Googleマップを初期化してaddMarker関数を作成するためのコードはすべてこの1つのファイルにあります(この巨大なコードはちょうどここに配置されています。次のコードをスキップして、それは):私のために働いていない

のsrc /プロバイダ/グーグル-maps.ts

import { Injectable } from '@angular/core'; 
import { Connectivity } from './connectivity'; 
import { Geolocation } from 'ionic-native'; 

/* 
    Generated class for the GoogleMaps provider. 

    See https://angular.io/docs/ts/latest/guide/dependency-injection.html 
    for more info on providers and Angular 2 DI. 
*/ 

declare var google; 

@Injectable() 

export class GoogleMaps { 

mapElement: any; 
pleaseConnect: any; 
map: any; 
mapInitialised: boolean = false; 
mapLoaded: any; 
mapLoadedObserver: any; 
markers: any = []; 
apiKey: string; 
styles: any; 

    constructor(public connectivityService: Connectivity) { 

    } 

    init(mapElement: any, pleaseConnect: any): Promise<any> { 

    this.mapElement = mapElement; 
    this.pleaseConnect = pleaseConnect; 

    return this.loadGoogleMaps(); 
    } 

    loadGoogleMaps(): Promise<any> { 

     return new Promise((resolve) => { 

     if(typeof google == "undefined" || typeof google.maps == "undefined") { 

      console.log("Google maps Javascript needs to be loaded"); 
      this.disableMap(); 

      if(this.connectivityService.isOnline()) { 

      window['mapInit'] =() => { 

       this.initMap().then(() => { 
       resolve(true); 
       }); 

       this.enableMap(); 
      } 

      let script = document.createElement("script"); 
      script.id = "googleMaps"; 

      if(this.apiKey) { 
       script.src = 'http://maps.google.com/maps/api/js?key=' + this.apiKey 
       + '&callback=mapInit'; 
      } else { 
       script.src = 'http://maps.google.com/maps/api/js?callback=mapInit'; 
      } 

      document.body.appendChild(script); 
      } 
     } 
     else { 
      if(this.connectivityService.isOnline()) { 
      this.initMap(); 
      this.enableMap(); 
      } else { 
      this.disableMap(); 
      } 
     } 

     this.addConnectivityListeners(); 
     }) 
    } 

    initMap(): Promise<any> { 

    this.mapInitialised = true; 

    return new Promise((resolve) => { 

     Geolocation.getCurrentPosition().then((position) => { 

     let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

     let mapOptions = { 
      center: latLng, 
      zoom: 15, 
     //mapTypeId: google.maps.MapTypeId.ROADMAP, -Doesn't seem necessary anymore 
      styles: [ 
    { 
    "featureType": "poi.business", 
    "stylers": [ 
     { 
     "visibility": "off" 
     } 
    ] 
    }, 
    { 
    "featureType": "road", 
    "elementType": "labels.icon", 
    "stylers": [ 
     { 
     "visibility": "off" 
     } 
    ] 
    }, 
    { 
    "featureType": "transit", 
    "stylers": [ 
     { 
     "visibility": "off" 
     } 
    ] 
    } 
] 
     } 

     this.map = new google.maps.Map(this.mapElement, mapOptions); 
     resolve(true); 
     }); 
    }); 
    } 

    disableMap(): void { 

    if(this.pleaseConnect) { 
     this.pleaseConnect.style.display = "block"; 
    } 

    } 

    enableMap(): void { 

    if(this.pleaseConnect) { 
     this.pleaseConnect.style.display = "none"; 
    } 
    } 

    addConnectivityListeners(): void { 

    document.addEventListener('online',() => { 

     console.log("online"); 

     setTimeout(() => { 

     if(typeof google == "undefined" || typeof google.maps == "undefined") { 
      this.loadGoogleMaps(); 
     } 
     else { 
      if(!this.mapInitialised) { 
      this.initMap(); 
      } 

      this.enableMap(); 
     } 

     },2000); 
    }, false); 
    } 

//Setting up custom Google Maps markers 

//iconBase: any = 'https://maps.google.com/mapfiles/kml/shapes/'; -Probably not necessary 


icons: any = { 
    partner: { 
    icon: 'partner.png' 
    }, 
    boughtFrom: { 
    icon: 'boughtFrom.png' 
    } 
} 

    addMarker(lat: number, lng: number, feature: any): void { 

    let latLng = new google.maps.LatLng(lat, lng); 

    let marker = new google.maps.Marker({ 
     map: this.map, 
     animation: google.maps.Animation.DROP, 
     position: latLng, 
     icon: this.icons[feature].icon 
    }); 

    this.markers.push(marker); 

    } 

} 

一部は、その最後の "addMarker()" 機能の "アイコン" の割り当てであります:

addMarker(lat: number, lng: number, feature: any): void { 

    let latLng = new google.maps.LatLng(lat, lng); 

    let marker = new google.maps.Marker({ 
     map: this.map, 
     animation: google.maps.Animation.DROP, 
     position: latLng, 
     icon: this.icons[feature].icon //Doesn't do anything 
    }); 

    this.markers.push(marker); 

    } 

現在、さまざまな種類のマーカーを別の場所で呼び出そうとしていますが、単にpartners.pngまたは{ url: 'partners.img' }に置き換えても、それでも何も認識されません。この問題場合

、これらはまた、私はそれが地図上のデフォルトのスタイルで表示されます使用している2つのテストマーカーである:

のsrc /資産/データ/ locations.json

{ 
    "locations": [ 

     { 
      "latitude": 40.79567309999999, 
      "longitude": -73.97358559999998, 
      "type": "partner" 
     }, 
     { 
      "latitude": 40.8107211, 
      "longitude": -73.95413259999998, 
      "type": "boughtFrom" 
     } 
    ] 
} 

私はこのすべての情報を統合したマップページも含まれます:

のsrc /ページ/ home.ts

import { Component, ElementRef, ViewChild } from '@angular/core'; 
import { Locations } from '../../providers/locations'; 
import { GoogleMaps } from '../../providers/google-maps'; 
import { NavController, Platform } from 'ionic-angular'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 

export class HomePage { 

@ViewChild('map') mapElement: ElementRef; 
@ViewChild('pleaseConnect') pleaseConnect: ElementRef; 

    constructor(public navCtrl: NavController, public maps: GoogleMaps, 
       public Platform: Platform, public locations: Locations) { 

    } 

    ionViewDidLoad() { 

    this.Platform.ready().then(() => { 

     let mapLoaded = this.maps.init(this.mapElement.nativeElement, this.pleaseConnect.nativeElement); 
     let locationsLoaded = this.locations.load(); 

     Promise.all([ 
     mapLoaded, 
     locationsLoaded 
     ]).then((result) => { 

     let locations = result[1]; 

     for(let location of locations) { 
      this.maps.addMarker(location.latitude, location.longitude, location.type); 
     } 
     }) 
    }); 
    } 

} 

ありがとうございました!

何かすべての助けがありがとうございます。

答えて

0

つまり、Ionic 2はカスタムマーカーのアイコン画像を定義できるGoogleマップJavascript APIで何を使用していますか?

何もありません。 Ionicはこれについて何ら責任を負いません。

私のテスト画像ファイルは、google-maps.tsと同じフォルダにあります(現在何が起きているのか分かります)。

これは問題です。ビルドプロセスでは、場所からtypescriptファイルを取り出し、1つのファイルにコンパイルします。 www/build/main.jsをビルドします。

これらの画像はmain.jsにはありません 画像をアセットフォルダに移動し、適切なパスを指定します。例えば

icon: 'assets/icon1.png' 
+1

うわー私は愚かな感じ、私は私がそれを試みたと思ったが、私は文字通り構文で間違った最後の時間を入力したように思えます。 これはすごくうまくいきます:)ありがとうmisha! – TheKomrad

関連する問題