通过比较它们的经度和纬度两个位置 [英] Comparing two locations using their Longitude and Latitude

查看:230
本文介绍了通过比较它们的经度和纬度两个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要onchangelocation()用来调用一个事件,通过比较当前的纬度和经度与一些保存的纬度和经度,但IM得到一个错误。日食是不承认关键词的距离,并为纠正错误是到箱子的方法距离为4的参数给出提示......如何解决它???或者一些其他的方式做同样的工作???

感谢和问候。 code连接低于

  @覆盖
公共无效onLocationChanged(位置定位){
 双currentLat = location.getLatitude();
 双currentLon = location.getLongitude();

    如果(距离(纬度,经度,currentLat,currentLon)< 2.0){
 //你想做什么?
  }
}
 

解决方案

您确实需要实现一个函数调用的距离,将计算两个位置之间的距离。的计算两个位置之间的距离为比较的经度和纬度值的一种可能的方式。

比较它们的一个例子:

  @覆盖
公共无效onLocationChanged(位置定位){

 双LAT2 = location.getLatitude();
 双lng2 = location.getLongitude();

    // LAT1和lng1是一个$ P $的值pviously存储位置
    如果(距离(LAT1,lng1,LAT2,lng2)< 0.1){//如果距离< 0.1英里我们把位置作为平等
       //你想做什么?
    }
}

/ **计算在MILES两个位置之间的距离* /
私人双距离(双LAT1,双lng1,双LAT2,双lng2){

    双earthRadius = 3958.75; //英里,更改为6371公里为输出

    双DLAT = Math.toRadians(LAT2-LAT1);
    双dLng = Math.toRadians(lng2-lng1);

    双sindLat = Math.sin(DLAT / 2);
    双sindLng = Math.sin(dLng / 2);

    双A = Math.pow(sindLat,2)+ Math.pow(sindLng,2)
        * Math.cos(Math.toRadians(LAT1))* Math.cos(Math.toRadians(LAT2));

    双c = 2 * Math.atan2(的Math.sqrt(a)中,的Math.sqrt(1-a))的;

    双DIST = earthRadius * C;

    返回DIST; //输出距离,英里
}
 

Hi i need to cal an event at onchangelocation(), by comparing current latitude and longitude with some saved latitude and longitude but i m getting an error. eclipse is not recognizing key word distance, and for correction error it is giving hint to crate method "distance" having 4 parameter ... how to fix it??? or some other way to do same work???

thanks and regards. code is attached below

@Override
public void onLocationChanged(Location location) {
 double currentLat=location.getLatitude();
 double currentLon=location.getLongitude();

    if (distance(lat,lon,currentLat,currentLon)<2.0){
 //do what you want to do...
  }
}

解决方案

You actually need to implement a function called distance that will calculate the distance between two locations. Calculating the distance between two locations is one possible way of comparing the longitude and latitude values.

An example of comparing them:

@Override
public void onLocationChanged(Location location) {

 double lat2 = location.getLatitude();
 double lng2 = location.getLongitude();

    // lat1 and lng1 are the values of a previously stored location
    if (distance(lat1, lng1, lat2, lng2) < 0.1) { // if distance < 0.1 miles we take locations as equal
       //do what you want to do...
    }
}

/** calculates the distance between two locations in MILES */
private double distance(double lat1, double lng1, double lat2, double lng2) {

    double earthRadius = 3958.75; // in miles, change to 6371 for kilometer output

    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);

    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);

    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
        * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    double dist = earthRadius * c;

    return dist; // output distance, in MILES
}

这篇关于通过比较它们的经度和纬度两个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆