android在谷歌地图上找到两点之间的距离和持续时间

时间:2018-01-24 07:18:34

标签: android google-maps

我跟着这里的啧啧https://inducesmile.com/android/android-find-distance-and-duration-between-two-points-on-android-map/

一切似乎都在起作用,我得到的只有2个错误以及跟随啧啧的所有人

首先在MapsActivity中

private List<LatLng> getDirectionPolylines(List<RouteObject> routes){
        List<LatLng> directionList = new ArrayList<LatLng>();
        for(RouteObject route : routes){
            List<LegsObject> legs = route.getLegs();
            for(LegsObject leg : legs){
                String routeDistance = leg.getDistance().getText(); HERE
                String routeDuration = leg.getDuration().getText(); HERE
                setRouteDistanceAndDuration(routeDistance, routeDuration); // here we will send the route Duration and distent
                List<StepsObject> steps = leg.getSteps();
                for(StepsObject step : steps){
                    PolylineObject polyline = step.getPolyline();
                    String points = polyline.getPoints();
                    List<LatLng> singlePolyline = decodePoly(points);
                    for (LatLng direction : singlePolyline){
                        directionList.add(direction);
                    }
                }
            }
        }
        return directionList;
    } 

错误

Cannot resolve method 'getText()

第二个错误是LegsObject类

import java.util.List;


public class LegsObject {
    private List<StepsObject> steps;

    private DistanceObject distance;

    private DurationObject duration;

    public LegsObject(DurationObject duration, DistanceObject distance, List<StepsObject> steps) {
        this.duration = duration;
        this.distance = distance;
        this.steps = steps;
    }

    public List<StepsObject> getSteps() {
        return steps;
    }

    public DistanceObject getDistance() {
        return distance;
    }

    public DurationObject getDuration() {
        return duration;
    }
}

错误

Cannot resolve symbol 'DistanceObject'
Cannot resolve symbol 'DurationObject'

我相信如果LegsObject.class中的第二个错误修复了第一个错误也将被修复

2 个答案:

答案 0 :(得分:1)

没有实施:

1)DistanceObject类

2)DurationObject类

<div class="template-benefits">
	<div class="tbenefitswrpr">
	<div class="tbenefits-left">
		<h1>Preventative maintenance of equipment and assets</h1>
		<ul>
			<li>Schedule recurring work orders for routine preventative maintenance for anything from computers, technology, and other appliances or equipment</li>
			<li>Easily view schedule of past completed maintenance work orders</li>
			<li>Custom dashboards to track preventative maintenance</li>
		</ul>
	</div>	
	<div class="tbenefits-right">
		<h1>Building and facility management</h1>
		<ul>
			<li>Manage multiple facilities all in one organized place</li>
			<li>Easily view past completed security and safety inspections</li>
			<li>Assign and coordinate work orders for repairs for utilities, plumbing, and broken appliances</li>
		</ul>
	</div>	
	</div>
</div>

持续时间相同的课程

public class DistanceObject{


    private Integer value;
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

获得距离

public class DurationObject {

    private String text;

    private Integer value;


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

获得持续时间

Integer disInMeters=routeA.getLegs().get(0).getDistance().getValue();
int  kilometers = (int) (disInMeters * 0.001); //convert in KM

获取更多帮助,请点击this link

答案 1 :(得分:1)

您可以使用第三方库。它简单而有效:

Gradle Dependancy:

compile 'com.akexorcist:googledirectionlibrary:1.0.4' // Custom Google Direction API \\

代码:

下面的方法将采用目的地的latLng,同样在方法内部你应该有包含你的原点的latlng对象。服务器密钥是您的api密钥,您还应该启用Google Directions API来实现此功能。

/**
     * Draw polyline on map, get distance and duration of the route
     *
     * @param latLngDestination LatLng of the destination
     */
    private void getDestinationInfo(LatLng latLngDestination) {
        progressDialog();
        String serverKey = getResources().getString(R.string.google_direction_api_key); // Api Key For Google Direction API \\
        final LatLng origin = new LatLng(latitude, longitude);
        final LatLng destination = latLngDestination;
        //-------------Using AK Exorcist Google Direction Library---------------\\
        GoogleDirection.withServerKey(serverKey)
                .from(origin)
                .to(destination)
                .transportMode(TransportMode.DRIVING)
                .execute(new DirectionCallback() {
                    @Override
                    public void onDirectionSuccess(Direction direction, String rawBody) {
                        dismissDialog();
                        String status = direction.getStatus();
                        if (status.equals(RequestResult.OK)) {
                            Route route = direction.getRouteList().get(0);
                            Leg leg = route.getLegList().get(0);
                            Info distanceInfo = leg.getDistance();
                            Info durationInfo = leg.getDuration();
                            String distance = distanceInfo.getText();
                            String duration = durationInfo.getText();

                            //------------Displaying Distance and Time-----------------\\
                            showingDistanceTime(distance, duration); // Showing distance and time to the user in the UI \\
//                            String message = "Total Distance is " + distance + " and Estimated Time is " + duration;
//                            StaticMethods.customSnackBar(consumerHomeActivity.parentLayout, message,
//                                    getResources().getColor(R.color.colorPrimary),
//                                    getResources().getColor(R.color.colorWhite), 3000);

                            //--------------Drawing Path-----------------\\
                            ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
                            PolylineOptions polylineOptions = DirectionConverter.createPolyline(getActivity(),
                                    directionPositionList, 5, getResources().getColor(R.color.colorPrimary));
                            googleMap.addPolyline(polylineOptions);
                            //--------------------------------------------\\

                            //-----------Zooming the map according to marker bounds-------------\\
                            LatLngBounds.Builder builder = new LatLngBounds.Builder();
                            builder.include(origin);
                            builder.include(destination);
                            LatLngBounds bounds = builder.build();

                            int width = getResources().getDisplayMetrics().widthPixels;
                            int height = getResources().getDisplayMetrics().heightPixels;
                            int padding = (int) (width * 0.20); // offset from edges of the map 10% of screen

                            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
                            googleMap.animateCamera(cu);
                            //------------------------------------------------------------------\\

                        } else if (status.equals(RequestResult.NOT_FOUND)) {
                            Toast.makeText(context, "No routes exist", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onDirectionFailure(Throwable t) {
                        // Do something here
                    }
                });
        //-------------------------------------------------------------------------------\\

    }