代码之家  ›  专栏  ›  技术社区  ›  nenur

locationservice ioic google maps对象在ios中失败,但在android中有效

  •  0
  • nenur  · 技术社区  · 7 年前

    我在android上发现,使用googlemapsjavascriptapi和ionic框架中的地理定位导致了速度非常慢或根本无法工作的问题。然后,我从以下文档中找到了Ionic的第二个地理定位模块locationservice: https://github.com/ionic-team/ionic-native-google-maps/blob/master/documents/locationservice/README.md 是的。我遇到的问题是,虽然这个定位模块是为android工作的,但它不是在ios上工作,也不是在本地主机上提供应用程序。下面是我得到的错误和设置地图中心的代码。

    map.html:地图:

    <ion-header>
      <ion-navbar>
        <ion-title>
          Map
        </ion-title>
      </ion-navbar>
    </ion-header>
    <ion-content>
      <div id='map'></div>
    </ion-content>
    

    地图.ts:

    import { Component, ViewChild, ElementRef } from '@angular/core';
    import { NavController, Platform, Navbar } from 'ionic-angular';
    import { Geolocation } from '@ionic-native/geolocation';
    import {
      LocationService,
      GoogleMap,
      GoogleMapOptions,
      MyLocation,
      GoogleMaps
    } from '@ionic-native/google-maps';
    
    declare var google: any;
    @Component({
      selector: 'page-map',
      templateUrl: 'map.html',
    })
    export class OfficeLocatorPage {
      @ViewChild(Navbar) navBar: Navbar;
      map: any;
      mapOptions:any;
      trafficEnabled = false;
      transitEnabled = false;
      bicycleEnabled = false;
      markers = [];
      places = [];
      trafficLayer = new google.maps.TrafficLayer();
      transitLayer = new google.maps.TransitLayer();
      bicycleLayer = new google.maps.BicyclingLayer();
      myLocation: any;
      infoWindow: any;
      isInfoWindowShown: boolean = false;
    
      constructor(private navCtrl: NavController, private platform: Platform, private geolocation: Geolocation) {
      }
    
      ionViewDidLoad() {
        this.navBar.backButtonClick = (e:UIEvent)=>{
          this.navCtrl.pop({animate: true, animation: "transition", direction: "left", duration: 300});
        };
     }
      ionViewDidEnter() {
          this.platform.ready().then(() => {
            this.places = [];
            this.initMap(this);
        });
      }
      initMap(scopeObj) {
        LocationService.getMyLocation({enableHighAccuracy: true}).then((location: MyLocation) => {
          this.setLocation(location.latLng);
        }).catch((error: any) => {
          // Can not get location, permission refused, and so on...
          console.log(error);
        });
      }
    
      setLocation(location) {
          this.map = new google.maps.Map(document.getElementById('map'), {
              center: location,
              zoom: 10,
              disableDefaultUI: true
          });
          let image = {
            url: "assets/icon/blue_dot.png", // url
            scaledSize: new google.maps.Size(25, 33), // scaled size
            origin: new google.maps.Point(0,0), // origin
            anchor: new google.maps.Point(0, 0) // ancho
          };
          let marker = new google.maps.Marker({
            position: location,
            map: this.map,
            icon: image
          });
          this.myLocation = new google.maps.LatLng(location.lat, location.lng);
        }
    

    错误日志:

    Error: Uncaught (in promise): TypeError: Cannot read property 'LocationService' of null
    TypeError: Cannot read property 'LocationService' of null
        at http://localhost:8100/build/vendor.js:78338:35
        at new t (http://localhost:8100/build/polyfills.js:3:21506)
        at Function.LocationService.getMyLocation 
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   nenur    7 年前

    这个错误似乎只在web浏览器中提供应用程序时才显示,xcode只需要清理和重建,所以地图现在就显示出来并工作了。我不相信在运行命令“ionic service”时它会在浏览器上工作,因为locationservice是一个ionic本机模块,除非应用程序安装在android或ios上,否则无法使用。