无法解析ComputeDistanceBetween方法

时间:2016-08-18 20:55:16

标签: java android google-maps android-fragments gps

我正在使用Google地图并支持地图片段类。我希望程序计算两点之间的距离。但是,我收到一条错误消息,说我无法解析ComputeDistanceBetween方法。我该如何解决? (大部分代码都来自Github)。

public class DistanceDemoActivity extends BaseDemoActivity implements GoogleMap.OnMarkerDragListener {
    private TextView mTextView;
    private Marker mMarkerA;
    private Marker mMarkerB;
    private Polyline mPolyline;
    private Object SphericalUtil;


    protected int getLayoutId() {
        return R.layout.distance_demo;
    }


    protected void startDemo() {
        mTextView = (TextView) findViewById(R.id.textView);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.8256, 151.2395), 10));
        getMap().setOnMarkerDragListener(this);

        mMarkerA = getMap().addMarker(new MarkerOptions().position(new LatLng(-33.9046, 151.155)).draggable(true));
        mMarkerB = getMap().addMarker(new MarkerOptions().position(new LatLng(-33.8291, 151.248)).draggable(true));
        mPolyline = getMap().addPolyline(new PolylineOptions().geodesic(true));

        Toast.makeText(this, "Drag the markers!", Toast.LENGTH_LONG).show();
        showDistance();
    }

   private void showDistance() {
        double distance = SphericalUtil.computeDistanceBetween(mMarkerA.getPosition(), mMarkerB.getPosition());
        mTextView.setText("The markers are " + formatNumber(distance) + " apart.");
    }

    private void updatePolyline() {
        mPolyline.setPoints(Arrays.asList(mMarkerA.getPosition(), mMarkerB.getPosition()));
    }

    private String formatNumber(double distance) {
        String unit = "m";
        if (distance < 1) {
            distance *= 1000;
            unit = "mm";
        } else if (distance > 1000) {
            distance /= 1000;
            unit = "km";
        }

        return String.format("%4.3f%s", distance, unit);
    }

    @Override
    public void onMarkerDragEnd(Marker marker) {
        showDistance();
        updatePolyline();
    }

    @Override
    public void onMarkerDragStart(Marker marker) {

    }

    @Override
    public void onMarkerDrag(Marker marker) {
        showDistance();
        updatePolyline();
    }
}

1 个答案:

答案 0 :(得分:1)

SphericalUtil是Google Maps API Utility Library中的一个类,您声明名为SphericalUtil的Object

删除此行:

private Object SphericalUtil;

另外,请确保您在instructions后面的build.gradle添加了Google Maps API工具库。