简体   繁体   中英

Ionic 2: Google Map Custom Controls Not Working

I have a google map implementation in Ionic2. Map and geolocation works fine. The errors occured only after I tried to implement custom controls/button into the map. I followed this link: https://developers.google.com/maps/documentation/javascript/examples/control-custom

This is my js code (Constructor):

 constructor(navController, platform, app) {
    this.navController = navController;
    this.platform = platform;
    this.app = app;

    platform.ready().then(() => {
    this.initializeMap();
    });
  }

Custom control:

 CenterControl(controlDiv, map){
    // Set CSS for the control border.
    var controlUI = document.createElement('div');
    controlUI.style.backgroundColor = '#fff';
    controlUI.style.border = '2px solid #fff';
    controlUI.style.borderRadius = '3px';
    controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
    controlUI.style.cursor = 'pointer';
    controlUI.style.marginBottom = '22px';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Click to recenter the map';
    controlDiv.appendChild(controlUI);

    // Set CSS for the control interior.
    var controlText = document.createElement('div');
    controlText.style.color = 'rgb(25,25,25)';
    controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
    controlText.style.fontSize = '16px';
    controlText.style.lineHeight = '38px';
    controlText.style.paddingLeft = '5px';
    controlText.style.paddingRight = '5px';
    controlText.innerHTML = 'Center Map';
    controlUI.appendChild(controlText);

    // Setup the click event listeners: simply set the map to Chicago.
    controlUI.addEventListener('click', function() {
      map.setCenter(chicago);
    });

  }

Map Initialization:

initializeMap() {

    let locationOptions = {timeout: 10000, enableHighAccuracy: true};
    navigator.geolocation.getCurrentPosition(

      (position) => {

        let options = {
          // /new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
          zoom: 16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        this.map = new google.maps.Map(document.getElementById("map_canvas"), options);

        var centerControlDiv = document.createElement('div');
        var centerControl = new CenterControl(centerControlDiv, options);
        centerControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);

      },

      (error) => {
        console.log(error);
      }, locationOptions
    );
  }

My index.html:

<script src="http://maps.google.com/maps/api/js?key=<MY API KEY>"></script>

The error message given was this: 在此处输入图片说明

Where line 87 refers to:

var centerControl = new CenterControl(centerControlDiv, options);

I'm unable to set "CenterControl(){}" as "function CenterControl(){}" as I would encounter syntax error in that case.

I would define CenterControl like this:

export class CenterControl {
  constructor(controlDiv, map){
    (...)
  }
}

instead of

CenterControl(controlDiv, map) {
  (...)
}

and import then when you want to use it... Don't forget the export keyword.

Edit

If it's a method of your class, simply call it this way:

this.CenterControl(centerControlDiv, options);
centerControlDiv.index = 1;
this.map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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