การค้นหาตำแหน่งที่ใกล้ที่สุดจากรายการพิกัด

ฉันต้องการค้นหาตำแหน่งที่ใกล้ที่สุดสำหรับที่อยู่ที่ระบุ (อินพุตของผู้ใช้) จากอาร์เรย์ของตำแหน่ง (lat, lng)

(หรือดีกว่านั้นให้จัดเรียงตามระยะทาง)

ฉันต้องการบรรลุเป้าหมายนี้โดยไม่ต้องแสดงแผนที่ ฉันไม่แน่ใจด้วยว่า Google API ใดเหมาะสมที่สุดสำหรับสิ่งนี้ (Geocoding API, Maps v3 ฯลฯ )

อะไรจะเป็นแนวทางที่ดีที่สุดสำหรับสิ่งนี้?

หากมีใครสามารถชี้ให้ฉันไปในทิศทางที่ถูกต้องได้ก็จะได้รับการชื่นชมอย่างมาก

สถานที่:

[
    {
        // Amsterdam
        "lat": 52.3702157,
        "lng": 4.8951679
    },
    {
        // Berlin
        "lat": 52.5200066,
        "lng": 13.404954
    },
    {
        // Brussels
        "lat": 50.85,
        "lng": 4.35
    },
    {
        // Paris
        "lat": 48.856614,
        "lng": 2.3522219
    },
    {
        // Madrid
        "lat": 40.4167754,
        "lng": -3.7037902
    }
]

อินพุตของผู้ใช้: Antwerp

{
    "lat": 51.2194475,
    "lng": 4.4024643
}

ควรกลับ: Brussels

{
    "lat": 50.85,
    "lng": 4.35
}

หรือจัดเรียงตามระยะทาง:

[
    {
        // Brussels
        "lat": 50.85,
        "lng": 4.35
    },
    {
        // Amsterdam
        "lat": 52.3702157,
        "lng": 4.8951679
    },
    {
        // Paris
        "lat": 48.856614,
        "lng": 2.3522219
    },
    {
        // Berlin
        "lat": 52.5200066,
        "lng": 13.404954
    },
    {
        // Madrid
        "lat": 40.4167754,
        "lng": -3.7037902
    }
]

person Chris    schedule 19.06.2014    source แหล่งที่มา
comment
developers.google.com/maps/documentation/javascript/   -  person Dr.Molle    schedule 20.06.2014
comment
I want to achieve this without displaying a map. ข้อกำหนดในการให้บริการประกอบด้วยส่วน (g) ห้ามใช้เนื้อหาโดยไม่มี Google Map คุณควรตรวจสอบว่าใบสมัครของคุณเป็นไปตามข้อกำหนดในการให้บริการ   -  person geocodezip    schedule 20.06.2014
comment
จะทำอย่างไรใน php ot html?   -  person MagikVishal    schedule 02.12.2015


คำตอบ (1)


Android มีฟีเจอร์ดีๆ สำหรับคุณ ซึ่งสามารถคำนวณระยะทางได้ (DistanceTo()) (อ้างอิง: http://developer.android.com/reference/android/location/Location.html) นี่คือตัวอย่างวิธีใช้ DistanceTo กับ google map api:

    private double CalculateDistance(LatLng endPoint)
    {
        //Convert LatLng to Location
        Location location = new Location("tmploc");
        location.Latitude = endPoint.Latitude;
        location.Longitude = endPoint.Longitude;
        location.Time = currentLocation.Time; //Set time as current position's time. (could also be datetime.now)
        return currentLocation.DistanceTo(location);
    }

จากนั้นคุณสามารถใช้สิ่งนี้เพื่อค้นหาค่าต่ำสุด:

public int Lowest(params int[] inputs)
{
  return inputs.Min();
}

(อ้างอิง: https://stackoverflow.com/a/4390808/3861983)

ถ้าคุณทำวิธีที่คุณใช้วิธีแรก ให้เพิ่มมันลงในอาร์เรย์แล้วรับตัวเลขที่ต่ำที่สุดจากวิธีที่สอง คุณจะบรรลุเป้าหมายของคุณ

person Wildcard    schedule 21.07.2014