2017-03-10 2 views
2

사용자의 현재 위치를 얻고이 위치와 다른 위치 사이의 거리를 계산하고 싶습니다. 이 자습서는 Create a Nearby Places List with Google Maps in Ionic 2입니다. 이 코드입니다 :IONIC 2에서 Javascript API를 사용하여 2 점 간의 거리를 계산하십시오.

let usersLocation; 
Geolocation.getCurrentPosition().then((position) => { 

    usersLocation = { lat :position.coords.latitude , lng : position.coords.longitude } 
    locations.map((location) => { 

    let placeLocation = { 
     lat: location.latitude, 
     lng: location.longitude 
    }; 

    location.distance = this.getDistanceBetweenPoints(
     usersLocation, 
     placeLocation, 
     'miles' 
    ).toFixed(2); 
    }); 

}); 

문제는 다음 usersLocation 변수는 위도와 현재 사용자의 위치보다의 경도로 설정되어 있지 않습니다. 너 나 좀 도와 줄 수있어 !!

+0

.then 함수는 getCurrentPosition을 호출 한 결과가 성공적 일 때만 사용됩니다. 오류 기능을 사용하여 오류를 잡는 좋은 방법이 있어야합니다 (https://developers.google.com/web/fundamentals/getting-started/primers/promises). 오류 함수는 getCurrentPosition()을 호출 할 때 발생할 수있는 오류를 판별하는 데 도움이됩니다. getCurrentPosition() 호출 오류가 발생하여 먹고 있습니다. –

+0

위의 링크에 표시된 방법을 사용했지만 성공 또는 오류 2 건을 입력하지 않았습니다. 다른 공급자에서 함수 getCurrentPosition()을 호출하고 있는데 제대로 작동합니다. – mehdi

+0

config.xml에 GPS –

답변

1
calculateDistance(lat1:number,lat2:number,long1:number,long2:number){ 
    let p = 0.017453292519943295; // Math.PI/180 
    let c = Math.cos; 
    let a = 0.5 - c((lat1-lat2) * p)/2 + c(lat2 * p) *c((lat1) * p) * (1 - c(((long1- long2) * p)))/2; 
    let dis = (12742 * Math.asin(Math.sqrt(a))); // 2 * R; R = 6371 km 
    return dis; 
    } 
관련 문제