简体   繁体   English

Google在Ionic2应用程序中的地图

[英]Google maps in Ionic2 application

I am building an Ionic2 app in which I have a google map. 我正在构建一个Ionic2应用程序,其中有一个Google地图。 I need to: 我需要:

  1. Draw a polygon 画一个多边形
  2. calculate it's area 计算面积
  3. should be able to delete the polygon 应该能够删除多边形

I basically need exactly the same solution as here: calculate area of a drawn polygon on google map javascript 我基本上需要与这里完全相同的解决方案: 计算google map javascript上绘制多边形的面积

So far I converted entire code in typescript and it's almost working. 到目前为止,我已将整个代码转换为打字稿,并且几乎可以正常工作。 However there is one issue: 但是,有一个问题:

  • When I am finished drawing the polygon for the first time, the area is shown on the UI. 当我第一次完成多边形的绘制时,该区域将显示在UI上。 If at this point, I modify my shape the area is not updated. 如果此时我修改了形状,则该区域不会更新。 see the gif image. 请参阅gif图片。
  • If I click anywhere on the map and then then again on the drawn shape and modify it, it's area is calculated for each successive modification. 如果我在地图上的任意位置单击,然后再次在绘制的形状上进行修改,则每次连续修改都会计算其面积。

What am I doing wrong? 我究竟做错了什么?

在此处输入图片说明

My component code: 我的组件代码:

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';


@Component({
  templateUrl: 'build/pages/start/start.html'
})

export class StartPage {

  selectedShape: any;

  constructor( private navController : NavController, private platform : Platform) {

    this.initializeMap();

  }

  clearSelection = (shape): void => {

    if(shape) {
      shape.setEditable(false);
      shape = null;
      this.selectedShape=shape
    }
  }

  setSelection = (shape): void => {

    this.clearSelection(shape);
    var shape = shape;
    this.selectedShape=shape;

    console.log(shape.getPath())

    shape.setEditable(true);
    google.maps.event.addListener(shape.getPath(), 'set_at', ()=>{this.calcar(shape)});
    google.maps.event.addListener(shape.getPath(), 'insert_at', ()=>{this.calcar(shape)});
  }

  calcar= (shape): void => {

    var shape = shape

    var area = google.maps.geometry.spherical.computeArea(shape.getPath());
    document.getElementById("area").innerHTML = "Area =" + area.toFixed(2);

    this.selectedShape=shape
  }

  deleteSelectedShape = (): void => {

    if (this.selectedShape) {
      this.selectedShape.setMap(null);
    }
  }

  initializeMap = (): void => {

    var newShape
    var map
    var drawingManager

    this.platform.ready().then(() => {
      var minZoomLevel = 15;

      map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: minZoomLevel,
        center: new google.maps.LatLng(52.5200, 13.4050),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        zoomControl: true    
      });

      var polyOptions = {
        strokeWeight: 0,
        fillOpacity: 0.45,
        editable: true
      };

      drawingManager = new google.maps.drawing.DrawingManager({
        drawingControl: true,
        drawingControlOptions: {
          drawingModes: [
          google.maps.drawing.OverlayType.POLYGON,   
          ]
        },
        polygonOptions: polyOptions,
        map: map
      });

      google.maps.event.addListener(drawingManager, 'overlaycomplete', (e) => {

        this.selectedShape=e.overlay

        if (e.type != google.maps.drawing.OverlayType.MARKER) {
      // Switch back to non-drawing mode after drawing a shape.
      drawingManager.setDrawingMode(null);

      // Add an event listener that selects the newly-drawn shape when the user
      // mouses down on it.
      newShape = e.overlay;
      newShape.type = e.type;

      google.maps.event.addListener(newShape, 'click', ()=> {

        this.setSelection(newShape);


      });

      var area = google.maps.geometry.spherical.computeArea(newShape.getPath());
      document.getElementById("area").innerHTML = "Area =" + area.toFixed(2);

      () => {this.setSelection(newShape);}
    }
  });

      google.maps.event.addListener(map, 'click',  ()=>{this.clearSelection(newShape);});
      google.maps.event.addDomListener(document.getElementById('delete-button'), 'click',  ()=>{this.deleteSelectedShape();});

    });

  }

}

Your mistake is located here: 您的错误位于此处:

  var area = google.maps.geometry.spherical.computeArea(newShape.getPath());
  document.getElementById("area").innerHTML = "Area =" + area.toFixed(2);

  () => {this.setSelection(newShape);} <== this line
}

You don't call setSelection function. 您不调用setSelection函数。 It is the same as: 它与:

function () {
  this.setSelection(newShape);
};

You need to use the following: 您需要使用以下内容:

this.setSelection(newShape);

See also the working plunker 另请参阅工作中的矮人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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