根據一個經緯度坐標獲取周邊最近經緯。Java實現


1.需求:指定一個經緯度坐標,如:(31.2121751783,121.4411213954)。周圍有一堆經緯度坐標,找出與它最近的那個。

2.實現思路:將給出經緯度看成原點(0,0)。周圍經緯度定位后利用三角形第三邊計算原點到各個經緯度的半徑距離,最短的那個即最近的經緯。

3.具體代碼:

package com.zjj.LatAndLongDemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.zjj.Entity.LocalAddress;


public class Test3 {

    private static List<LocalAddress> ilist = new ArrayList<LocalAddress>();
    public static void main(String[] args) {
        Test3 test3 = new Test3();
        Double localDouble  = 0.0;
    
//定義一個二維數組存放經緯度
        Double[][] doubles = { { 31.1963877723, 121.4940175770 },
                { 31.2020280000, 121.5006010000 },
                { 31.1963702573, 121.4940084124 },
                { 31.1951946273, 121.4991236524 },
                { 31.1983746273, 121.4895436524 },
                { 31.2068062375, 121.4686363819 },
                { 31.2127140000, 121.4751610000 },
                { 31.2067706666, 121.4686028298 },
                { 31.2056732366, 121.4737227198 },
                { 31.2087332366, 121.4640927198 },
                { 31.2103126101, 121.4457401593 },
                { 31.2166680000, 121.4521640000 } };
        //門店的經緯度
        Double[] initlocal = {31.1221751783,121.5011213954 };

        
        for (int i = 0; i < doubles.length; i++) {
            System.out.println("(" + doubles[i][0] + "," + doubles[i][1] + ")");
            Double z = test3.GetShotLocal(doubles[i][0], doubles[i][1],initlocal[0], initlocal[1]);        
            System.out.println(z);
            //獲取最短距離后把經緯度和距離存放到對象中
            LocalAddress localaddress = new LocalAddress(doubles[i][0], doubles[i][1], z);
            //將對象用list保存
            ilist.add(localaddress);            
        }
        
        List<LocalAddress> shotlocal =  getLocalList();
        Double lat=shotlocal.get(0).getLat();
        Double lon= shotlocal.get(0).getLon();
        localDouble = shotlocal.get(0).getDistance();
        System.err.println("最近的距離:" + localDouble + "。對應的經緯是:" +"(" + lat + "," + lon + ")");
    }

    /**
     * 獲取最短的距離
     * @param lat
     * @param lon
     * @param initlat
     * @param initlon
     * @return
     */
    public Double GetShotLocal(Double lat, Double lon, Double initlat,Double initlon) {
        Double x = (initlat - lat) * (initlat - lat);
        Double y = (initlon - lon) * (initlon - lon);
        Double z = Math.sqrt(x + y);
        return z;
    }
    
    /**
     * 對List<LocalAddress> 進行排序
     * @return
     */
    public static List<LocalAddress> getLocalList()  {
        Collections.sort(ilist, new Comparator<LocalAddress>() {
            @Override
            public int compare(LocalAddress arg0, LocalAddress  arg1) {
                Double double1 = arg0.getDistance();
                Double double2 = arg1.getDistance();
                if(double1>double2){
                    return 1;
                }else if (double1 == double2) {
                    return 0;
                }else {
                    return -1;
                }
            }
        });
        return ilist;
    }
}

 

LocalAddress類:

package com.zjj.Entity;

public class LocalAddress {

    private Double lat;
    private Double lon;
    private Double distance;
    
    public LocalAddress() {
        // TODO Auto-generated constructor stub
    }
    
    public LocalAddress(Double lat, Double lon, Double distance) {
        super();
        this.lat = lat;
        this.lon = lon;
        this.distance = distance;
    }
    public Double getLat() {
        return lat;
    }
    public void setLat(Double lat) {
        this.lat = lat;
    }
    public Double getLon() {
        return lon;
    }
    public void setLon(Double lon) {
        this.lon = lon;
    }
    public Double getDistance() {
        return distance;
    }
    public void setDistance(Double distance) {
        this.distance = distance;
    }
       
}

4.效果:

565256-20161228104159445-1722795

為了方便查看效果這里給出兩組簡單數據,代碼中是真實經緯度。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM