5

dữ liệu Với vị trí thời gian thực của một thiết bị di chuyển qua một khoảng thời gian ngắn, làm thế nào tôi có thể nhận được một cặp lat/dài hơn nữa dọc theo con đường này, nói rằng, 2 dặm, hoặc thậm chí tốt hơn, 5 phút giá trị của lái xe?Làm cách nào để ngoại suy xa hơn trên một con đường?

Tôi thấy rằng tôi có thể sử dụng API đường của Google để chụp một con đường được cung cấp cho một cặp lat/long https://developers.google.com/maps/documentation/roads/snap, nhưng điều đó chỉ giúp tôi chia sẻ ở đó.

Điều này sẽ có trong ứng dụng Android.

+0

Liệu không google hướng API có một số loại giải pháp cho rằng https: //developers.google.com/maps/documentation/directions/intro#Waypoints – commonSenseCode

+0

Không phải là tôi có thể thấy – Ken

+0

tôi nghĩ rằng bạn đang tìm kiếm https://developer.android.com/reference/android/telephony/TelephonyManager.html#getCellLocation() –

Trả lời

5

Cho một đường biểu diễn như một List<LatLng>, chức năng này sẽ tính toán các điểm trên tuyến đường mà kết quả từ đi du lịch từ origindistance mét trên tuyến đường đó (bằng cách sử dụng Google Maps API Utility Library để làm một số tính toán):

private LatLng extrapolate(List<LatLng> path, LatLng origin, float distance) { 
    LatLng extrapolated = null; 

    if (!PolyUtil.isLocationOnPath(origin, path, false, 1)) { // If the location is not on path non geodesic, 1 meter tolerance 
     return null; 
    } 

    float accDistance = 0f; 
    boolean foundStart = false; 
    List<LatLng> segment = new ArrayList<>(); 

    for (int i = 0; i < path.size() - 1; i++) { 
     LatLng segmentStart = path.get(i); 
     LatLng segmentEnd = path.get(i + 1); 

     segment.clear(); 
     segment.add(segmentStart); 
     segment.add(segmentEnd); 

     double currentDistance = 0d; 

     if (!foundStart) { 
      if (PolyUtil.isLocationOnPath(origin, segment, false, 1)) { 
       foundStart = true; 

       currentDistance = SphericalUtil.computeDistanceBetween(origin, segmentEnd); 

       if (currentDistance > distance) { 
        double heading = SphericalUtil.computeHeading(origin, segmentEnd); 
        extrapolated = SphericalUtil.computeOffset(origin, distance - accDistance, heading); 
        break; 
       } 
      } 
     } else { 
      currentDistance = SphericalUtil.computeDistanceBetween(segmentStart, segmentEnd); 

      if (currentDistance + accDistance > distance) { 
       double heading = SphericalUtil.computeHeading(segmentStart, segmentEnd); 
       extrapolated = SphericalUtil.computeOffset(segmentStart, distance - accDistance, heading); 
       break; 
      } 
     } 

     accDistance += currentDistance; 
    } 

    return extrapolated; 
} 

Dưới đây là một số xét nghiệm:

List<LatLng> route = new ArrayList<>(); 
route.add(new LatLng(40, 4)); 
route.add(new LatLng(40.1, 4)); 
route.add(new LatLng(40.1, 4.1)); 
route.add(new LatLng(40.2, 4.1)); 
route.add(new LatLng(40.2, 4.2)); 
route.add(new LatLng(40.3, 4.2)); 
route.add(new LatLng(40.3, 4.3)); 

LatLng origin; 
LatLng extrapolated; 

origin = new LatLng(40.1, 4.05); 
extrapolated = extrapolate(route, origin, 30000); 
Log.e("Extrapolated1", "" + extrapolated); // lat/lng: (40.25517043951189,4.2) 

origin = new LatLng(40.05, 4); 
extrapolated = extrapolate(route, origin, 100); 
Log.e("Extrapolated2", "" + extrapolated); // lat/lng: (40.05089932033549,4.0) 

origin = new LatLng(40.05, 4); 
extrapolated = extrapolate(route, origin, 50000); 
Log.e("Extrapolated3", "" + extrapolated); // lat/lng: (40.300010207449226,4.261348349980259) 

origin = new LatLng(40.05, 4); 
extrapolated = extrapolate(route, origin, 100000); 
Log.e("Extrapolated4", "" + extrapolated); // null (result out of the route) 
+0

Câu trả lời tuyệt vời này! Thật không may, tôi không có lộ trình. Tôi đã chỉnh sửa câu hỏi để làm rõ điều này. – Ken

+1

Tôi sẽ chỉnh sửa câu trả lời của mình nếu tôi tìm thấy một cái gì đó phù hợp với nhu cầu của bạn, nhưng tôi sẽ giữ phương pháp này trong trường hợp nó có thể giúp người khác của bạn không phiền – antonio

Các vấn đề liên quan