0

그래서 Google Maps 및 Ionic 2에서 작업하고 있습니다. Cordova에는 사용자의 위치 정보를 검색 할 수있는 플러그인이 있으며 플러그인에 대해 두 가지 질문이 있습니다. watch.unsubscribe()Geolocation.clearWatch(watch) : 내 질문에 언급하기 전에ionic 2 Geolocation clearWatch 또는 unsubscribe

는하지만, 여기에 사용자의 위치를보고 중지하는 방법은 두 가지가 있습니다, 내 코드

import { Geolocation } from 'ionic-native'; 

watchPosition(marker) { 
    let watch = Geolocation.watchPosition({ 
     //enableHighAccuracy: true 
    }); 

    watch.subscribe((pos) => { 
     marker.setPosition({ 
      lat: pos.coords.latitude, 
      lng: pos.coords.longitude 
     }); 
    }); 

    // stop watching if the user starts dragging the map 
    this.map.addListener('dragstart',() => { 
     // There might be two ways to do this, but non of them works 
     watch.unsubscribe(); 
     Geolocation.clearWatch(watch); 
    }); 
} 
  1. AFAIK입니다. 그러나 unsubscribe의 유형이 Observable이고 다른 하나는 Geolocation 플러그인에서 가져온 차이점이 무엇인지 실마리가 없습니다. 어느 것을 사용해야합니까?

  2. 위의 질문은 실제로 사소한 것으로, 지금 내가 가진 가장 중요한 문제는 둘 다 작동하지 않는다는 것입니다. watch.unsubscribe()[ts] Property 'unsubscribe' does not exist on type 'Observable<Geoposition>'.Geolocation.clearWatch(watch)이주는 오류를 알려주십시오 [ts] Property 'clearWatch' does not exist on type 'typeof Geolocation'. 뭔가가 있습니까?

답변

1

당신이 geolocation pluginIonic Native Typescript wrapper 보면, 당신은 볼 수 있습니다 :

  • 그것은 clearWatch() 기능을 노출하지 않습니다 직접
  • its watchPosition() wrapper 누구 unsubscribe() 작업입니다 clearWatch()를 호출 할 수있는 관찰 가능한을 반환 내부는 watchId 인 geolocation 플러그인에 있습니다. 따라서 시계를 지우기위한 이온 네이티브 방법은 Geolocation.watchPosition()에 의해 반환 된 Observable의 unsubscribe()을 호출하는 것입니다.

그러나 작동하지 않습니다 어떤 이유로, 당신은 단지 직접 플러그인 API를 호출 할 수있는 경우 :

declare var navigator: any; 

// Add watch 
let watchId = navigator.geolocation.watchPosition((position) => { 
    // do something with position 
} , (error) => { 
    // do something with error 
}), options); 

// Clear watch 
navigator.geolocation.clearWatch(watchId); 
+0

감사합니다! 그게 내 질문에 대한 답이야. BTW, 나는 당신이 마지막 줄에서'clearWatch (watchId)'를 의미했다고 생각한다. – Coffee

+0

당신 말이 맞아요. Typo-tastic ☺️ 고정. – DaveAlden

+0

ionic의 위치 정보 watchPosition에서 반환되는 관찰 가능 항목에 대한 구독 취소 방법이 없을 때 우리는 무엇을합니까? – Jahrenski