检查路线是否包含特定坐标 [英] Check if route contains a specific coordinates or not

查看:71
本文介绍了检查路线是否包含特定坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,其中我想知道一条路线是否包含一组特定的经纬度坐标.这是我的代码:

I am currently developing an application in which i want to know if a route contains a certain set of lat long coordinates. Here is my code:

public class PathActivity extends FragmentActivity implements OnMapReadyCallback, RoutingListener {

    private GoogleMap mMap;
    FetchLocation fetchLocation;

    LatLng start;
    LatLng end;
    ProgressDialog pd;
    List<Polyline> polylines;
    private static final int[] COLORS = new int[]{R.color.gradient_dark_pink};

    FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_path);

        //Receiving Object From Intent
        Intent rcv = getIntent();
        fetchLocation = (FetchLocation) rcv.getSerializableExtra("keyFetchLocationObject2");

        pd = new ProgressDialog(this);
        pd.setMessage("Please Wait...");

        firestore = FirebaseFirestore.getInstance();

        fetchAllTrafficLights();

        polylines = new ArrayList<>();

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

       pd.show();

       //Making a Path
        start = new LatLng(fetchLocation.latitude, fetchLocation.longitude);
        end = new LatLng(fetchLocation.destinationLatitude, fetchLocation.destinationLongitude);
        Routing routing = new Routing.Builder()
                .travelMode(Routing.TravelMode.DRIVING)
                .withListener(this)
                .alternativeRoutes(false)
                .waypoints(start, end)
                .build();

        routing.execute();
    }

    @Override
    public void onRoutingFailure(RouteException e)
    {
        Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
        pd.dismiss();
    }

    @Override
    public void onRoutingStart() {

    }

    @Override
    public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex)
    {
        pd.dismiss();

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, 16));

        if(polylines.size()>0) {
            for (Polyline poly : polylines) {
                poly.remove();
            }
        }

        polylines = new ArrayList<>();
        //add route(s) to the map.
        for (int i = 0; i <route.size(); i++)
        {
            //In case of more than 5 alternative routes
            int colorIndex = i % COLORS.length;

            PolylineOptions polyOptions = new PolylineOptions();
            polyOptions.color(getResources().getColor(COLORS[colorIndex]));
            polyOptions.width(10 + i * 3);
            polyOptions.addAll(route.get(i).getPoints());
            Polyline polyline = mMap.addPolyline(polyOptions);
            polylines.add(polyline);

            Toast.makeText(getApplicationContext(),"Route "+ (i+1) +": distance - "+ route.get(i).getDistanceValue()+": duration - "+ route.get(i).getDurationValue(),Toast.LENGTH_SHORT).show();

            // Start marker
            MarkerOptions options = new MarkerOptions();
            options.position(start);
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_start_blue));
            mMap.addMarker(options);

            // End marker
            options = new MarkerOptions();
            options.position(end);
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_end_green));
            mMap.addMarker(options);


        }

    }

    @Override
    public void onRoutingCancelled() {

    }

    public void fetchAllTrafficLights()
    {
        pd.show();
        firestore.collection("Controller").get().addOnCompleteListener(this, new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(Task<QuerySnapshot> task)
            {
                if(task.isSuccessful())
                {
                    for(QueryDocumentSnapshot documentSnapshot : task.getResult())
                    {
                        Log.i("Hello", documentSnapshot.get("controllerLatitude").toString() + "   " + documentSnapshot.get("controllerLongitude").toString());
                        pd.dismiss();
                    }
                }
            }
        })
                .addOnFailureListener(this, new OnFailureListener()
                {
                    @Override
                    public void onFailure(Exception e)
                    {
                        Toast.makeText(PathActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        pd.dismiss();
                    }
                });
    }
}

我正在使用github库: https://github.com/jd-alexander /Google-Directions-Android 绘制两点之间的路线. 已协调的对象已保存在Firestore数据库中,并已成功获取,如Log中所示.现在,我要检查从数据库中获取的纬度长点是否在路径中.例如.如果我们从A点移动到D点,我想检查路线上是否存在B,C点.我还想知道Google Places API是否始终在两个位置之间给出相同的路线坐标.这是我的对象:

I am using github library: https://github.com/jd-alexander/Google-Directions-Android to draw the route between two points. The coordinated are already saved in the firestore database and are successfully fetched as shown in Log. Now I want to check if the lat long points fetched from database are in the path or not. Eg. If we move from point A to D, I want to check points B,C are present on the route or not. I also want to know does the google places api always give same route coordinates between two locations. Here is my object:

public class FetchLocation implements Serializable
{
    public double latitude;
    public double longitude;
    public double destinationLatitude;
    public double destinationLongitude;

    public FetchLocation()
    {

    }

    public FetchLocation(double latitude, double longitude, double destinationLatitude, double destinationLongitude) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.destinationLatitude = destinationLatitude;
        this.destinationLongitude = destinationLongitude;
    }

    @Override
    public String toString() {
        return "FetchLocation{" +
                "latitude=" + latitude +
                ", longitude=" + longitude +
                ", destinationLatitude=" + destinationLatitude +
                ", destinationLongitude=" + destinationLongitude +
                '}';
    }
}

在上一个活动中,使用google place自动完成获取了用户来源经纬度- https ://developers.google.com/places/android-sdk/autocomplete 并在传递给此活动的对象中进行设置.

The users source lat long are fetched in the previous activity using google place autocomplete- https://developers.google.com/places/android-sdk/autocomplete and are set in the object which is passed to this activity.

任何人都请帮忙!

推荐答案

看看 Google Maps Android API实用程序库.您需要获取从A到D的折线路径,并使用isLocationOnPath()检查列表中的每个点(B和C)(如果它位于A-D路径上).像这样的东西:

Take a look at PolyUtil.isLocationOnPath(LatLng point, java.util.List<LatLng> polyline, boolean geodesic, double tolerance) method of Google Maps Android API Utility Library. You need to get polyline path from A to D and check each point from list (B and C) with isLocationOnPath() if it laying on A-D path. Something like that:

for (LatLng point : pointsBandCList) {
    if (PolyUtil.isLocationOnPath(point, polylineFromAtoD.getPoints(), true, 100)) {
        // "point" laying on A to D path

        ...
    }
}

其中100-是公差(以米为单位).您可以根据自己的任务进行调整.

where 100 - is tolerance (in meters). You can adjust it for your task.

这篇关于检查路线是否包含特定坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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