显示在地图上的经纬度codeR最接近的结果 [英] Show closest result of a Geocoder on map

查看:127
本文介绍了显示在地图上的经纬度codeR最接近的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在这一问题。我在寻找乐购门店在这个地理codeR。反正是有得到geo.getFromLocationName只有最接近结果的方式吗?

 私人无效setUpMap()抛出IOException异常{
   地理codeR地理=新的地缘codeR(getApplicationContext(),Locale.getDefault());
    名单<地址> AddressList中= geo.getFromLocationName(乐购,1);
    地址添加= addressList.get(0);
    串局部性= add.getLocality();

    双纬度= addressList.get(0).getLatitude();
    双LNG = addressList.get(0).getLongitude();


    (新MarkerOptions()位置(新的经纬度(纬度,经度))标题(维特罗斯)。)mMap.addMarker;
}
 

解决方案

修改你的方法是这样的:

 私人无效setUpMap()抛出IOException异常{
    地理codeR地理=新的地缘codeR(getApplicationContext(),Locale.getDefault());
    名单<地址> AddressList中= geo.getFromLocationName(乐购,1);
    地址yourAddress = //得到你的位置或地址进行比较

    地址最接近= findClosest(AddressList中,yourAddress);
    //做你的需要
}
 

要创建 findClosest ,你必须创建一个函数,遍历结果和使用的半正矢公式来计算您的位置的距离(或所需的)。

 公共双弧度(双X)
{
    返回X * Math.PI / 180;
}

公共地址findClosest(名单<地址> AddressList中,地址yourAddress)
{
    双纬度= yourAddress.getLatitude(); //你的(或需要)纬度
    双LNG = yourAddress.getLongitude(); //你的(或需要)经度

    双R = 6371; //地球的公里半径范围
    双[]距离=新的双[addressList.lenght]
    变种最接近= -1;
    对于(i = 0; I< addressList.lenght;我++){
        双MLAT = addressList.get(我).getLatitude();
        双MLNG = addressList.get(ⅰ).getLongitude();
        双DLAT = RAD(MLAT  -  LAT);
        双dLong = RAD(MLNG  - 液化天然气);
        双A = Math.sin(DLAT / 2)* Math.sin(DLAT / 2)+
            Math.cos(弧度(LAT))* Math.cos(弧度(LAT))* Math.sin(dLong / 2)* Math.sin(dLong / 2);
        双c = 2 * Math.atan2(的Math.sqrt(a)中,的Math.sqrt(1-a))的;
        双D = R * C;
        距离[我] = D;
        如果(最接近的== -1 || D<距离[接近]){
            最接近=我;
        }
    }

    返回addressList.get(最近);
}
 

I've been stuck on the problem. i'm searching for Tesco stores in this geocoder. Is there anyway way of getting only the closest result of the geo.getFromLocationName?

  private void setUpMap() throws IOException {
   Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
    List<Address> addressList= geo.getFromLocationName("Tesco",1);
    Address add = addressList.get(0);
    String locality = add.getLocality();

    double lat = addressList.get(0).getLatitude();
    double lng = addressList.get(0).getLongitude();


    mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title("Waitrose"));
}

解决方案

Modify your method like this:

private void setUpMap() throws IOException {
    Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
    List<Address> addressList= geo.getFromLocationName("Tesco",1);
    Address yourAddress = // get your location or the address to compare

    Address closest = findClosest(addressList, yourAddress);
    // do what you need
}

To create the findClosest you have to create a function that iterates over results and use haversine formula to calculate the distance to your location (or the desired one).

public double rad(double x) 
{
    return x*Math.PI/180.;
}

public Address findClosest( List<Address> addressList, Address yourAddress ) 
{
    double lat =  yourAddress.getLatitude(); // your (or desired) latitude
    double lng =  yourAddress.getLongitude(); // your (or desired) longitude

    double R = 6371.; // radius of earth in km
    double[] distances = new double[addressList.lenght];
    var closest = -1;
    for( i=0;i<addressList.lenght; i++ ) {
        double mlat = addressList.get(i).getLatitude();
        double mlng = addressList.get(i).getLongitude();
        double dLat  = rad(mlat - lat);
        double dLong = rad(mlng - lng);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double d = R * c;
        distances[i] = d;
        if ( closest == -1 || d < distances[closest] ) {
            closest = i;
        }
    }

    return addressList.get(closest);
}

这篇关于显示在地图上的经纬度codeR最接近的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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