Android指向两点之间的方向

时间:2018-06-14 11:51:48

标签: android

我正在制作地图应用程序,我在两个点​​之间显示路线。

在此我从附近的地方(Google)检索纬度和经度。到目前为止一切都很好,但我没有得到指示。它显示了地图中的回合点,但没有任何反应。

我检查了控制台,我没有错误。我已经在Google上寻找解决方案,但到目前为止我还没有找到任何解决方案。我已经尝试调试代码了。

这是ViewDirections代码:

private GoogleMap mMap;

FusedLocationProviderClient fusedLocationProviderClient;
LocationCallback locationCallback;
LocationRequest locationRequest;
Location mLastLocation;
Marker mCurrentMarker;

Polyline polyline;

IGoogleAPIService mService;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_directions);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    mService = Common.getGoogleAPIServiceScalars();

    buildLocationRequest();
    buildLocationCallBack();


    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}

@Override
protected void onStop() {
    fusedLocationProviderClient.removeLocationUpdates(locationCallback);
    super.onStop();
}

private void buildLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(1000);
    locationRequest.setSmallestDisplacement(10f);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}

private void buildLocationCallBack() {
    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            mLastLocation = locationResult.getLastLocation();
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()))
                    .title("Your position")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            mCurrentMarker = mMap.addMarker(markerOptions);

            mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude())));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));


            //create marker destination
            LatLng destinationLatLng = new LatLng(Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLat()),
                    Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLng()));


            mMap.addMarker(new MarkerOptions()
                    .position(destinationLatLng)
                    .title(Common.currentResult.getName())
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

            drawPath(mLastLocation,Common.currentResult.getGeometry().getLocation());
        }
    };
}


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

    mMap.getUiSettings().setZoomControlsEnabled(true);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            mLastLocation = location;
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()))
                    .title("Your position")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            mCurrentMarker = mMap.addMarker(markerOptions);

            mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude())));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));


            //create marker destination
            LatLng destinationLatLng = new LatLng(Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLat()),
                     Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLng()));


            mMap.addMarker(new MarkerOptions()
                    .position(destinationLatLng)
                    .title(Common.currentResult.getName())
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

            drawPath(mLastLocation,Common.currentResult.getGeometry().getLocation());

        }
    });
}

private void drawPath(Location mLastLocation, com.example.osim4.travelonbucovina.Model.Location location) {
    //clear all polyline
    if(polyline !=null)
        polyline.remove();

    String origin = new StringBuilder(String.valueOf(mLastLocation.getLatitude())).append(",").append(String.valueOf(mLastLocation.getLongitude()))
            .toString();
    String destination = new StringBuilder(location.getLat()).append(",").append(location.getLng())
            .toString();

    mService.getDirections(origin,destination)
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    new ParserTask().execute(response.body().toString());

                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {

                }
            });
}

private class ParserTask extends AsyncTask<String,Integer,List<List<HashMap<String,String>>>> {
    AlertDialog waitingDialog = new SpotsDialog(ViewDirections.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        waitingDialog.show();
        waitingDialog.setMessage("Please waiting ....");
    }

    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
        JSONObject jsonObject;
        List<List<HashMap<String, String>>> routes = null;
        try{
            jsonObject = new JSONObject(strings[0]) ;
            DirectionJSONParser parser = new DirectionJSONParser();
            routes = parser.parse(jsonObject);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return routes;
    }

    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> lists) {
        super.onPostExecute(lists);

        ArrayList points = null;
        PolylineOptions polylineOptions = null;

        for(int i=0;i<lists.size();i++)
        {
            points = new ArrayList();
            polylineOptions = new PolylineOptions();

            List<HashMap<String,String>> path = lists.get(i);

            for(int j=0;j<lists.size();j++)
            {
                HashMap<String,String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat,lng);

                points.add(position);
            }

            polylineOptions.addAll(points);
            polylineOptions.width(12);
            polylineOptions.color(Color.RED);
            polylineOptions.geodesic(true);

        }
        polyline = mMap.addPolyline(polylineOptions);
        waitingDialog.dismiss();
    }
}

0 个答案:

没有答案