简体   繁体   English

离子2 - 宣传单地图onclick事件

[英]Ionic 2 - Leaflet map onclick event

I'm building my very first app using Ionic 2. 我正在使用Ionic 2构建我的第一个应用程序。

Note: I've already build the app, correctly loaded the map using "leaflet". 注意:我已经构建了应用程序,使用“传单”正确加载了地图。

Problem : I want my Leaflet class to get map click position in order to save it. 问题 :我希望我的Leaflet类获取地图点击位置以保存它。

But, If I use map.on('click', function) event provided by leaflet library, I can't access the class object. 但是,如果我使用由传单库提供的map.on('click', function)事件,我无法访问类对象。

I need that var clickPosition = e.latlang; 我需要var clickPosition = e.latlang; to be accesible from class scope. 可从类范围访问。 How to do that? 怎么做?

If I use (click) listener provided by Ionic, I can't get event.latlng properties needed to save the click position on map. 如果我使用Ionic提供的(click)监听器,我无法获得在地图上保存点击位置所需的event.latlng属性。

I'm sure it's a problem os var scoping, but as I'm quite a newbie on angular and Ionic I need someone to show me the correct direction. 我确定这是一个问题os var范围,但因为我是角度和离子的新手我需要有人向我展示正确的方向。

leaflet.ts leaflet.ts

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

declare var L: any;


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

    constructor(public navCtrl: NavController, public navParams: NavParams) {}

    ionViewDidLoad() { 

        console.log('ionViewDidLoad LeafletPage');

        // create the slippy map
        var map = L.map('mapLeaflet', {
            minZoom: 1,
            maxZoom: 4,
            center: [0, 0],
            zoom: 1,
            crs: L.CRS.Simple
        });

        // dimensions of the image
        var w = 5161,
            h = 3385,
            url = './assets/images/mapa.jpg';

        // calculate the edges of the image, in coordinate space
        var southWest = map.unproject([0, h], map.getMaxZoom() - 1);
        var northEast = map.unproject([w, 0], map.getMaxZoom() - 1);
        var bounds = new L.LatLngBounds(southWest, northEast);

        // add the image overlay, so that it covers the entire map
        L.imageOverlay(url, bounds).addTo(map);

        // tell leaflet that the map is exactly as big as the image
        map.setMaxBounds(bounds);

        /****************************************************************
            tempMarker
        ******************************************************************/

        var tempIcon = L.icon({
            iconUrl: './assets/icon/pin.png',
            shadowUrl: '',
            iconSize:     [64, 64], // size of the icon
            shadowSize:   [0, 0], // size of the shadow
            iconAnchor:   [32, 64], // point of the icon which will correspond to markers location
            shadowAnchor: [0, 0],  // the same for the shadow
            popupAnchor:  [32, 20] // point from which the popup should open relative to the iconAnchor
        });

        map.on('click', onMapClick);

        var tempMarker;

        function onMapClick(e) {
                var clickPosition = e.latlang;
                var tempMarker = L.marker(e.latlng, {icon: tempIcon})
                .on('click', showMarkerMenu)
                .addTo(map);
       }

       function showMarkerMenu(e){
            alert('crear nueva incidencia ' + e.latlng);
            //this.navCtrl.push(FormPage);
       }

    }

}

leaflet.html leaflet.html

<ion-header>

  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Mapa Leaflet</ion-title>
  </ion-navbar>

</ion-header>

<ion-content>
  <div id='mapLeaflet' class="mapLeaflet" ></div> 
</ion-content>

instead of 代替

map.on('click', onMapClick);

use 采用

map.on('click', (e)=>{this.onMapClick(e)});

to check use 检查使用

onMapClick(e) {
    console.log(e.latlng.lng, e.latlng.lat);
....
}

Just if someone is in this same situation, I accomplished this reestructuring my app and using this starter proyect as template: 如果有人处于同样的情况,我完成了这个重构我的应用程序并使用此启动程序proyect作为模板:

https://github.com/haoliangyu/angular2-leaflet-starter https://github.com/haoliangyu/angular2-leaflet-starter

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

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