Android在两点之间指出方向

Android在两点之间指出方向,android,Android,我正在制作一个地图应用程序,显示两点之间的路线 在这篇文章中,我从附近的地方(谷歌)检索纬度和经度。到目前为止一切都很好,但我不知道方向。它在地图上显示了布特点,但那个里什么也并没有发生 我检查了控制台,没有错误。我在谷歌上寻找了一个解决方案,但到目前为止我什么也没找到。我已经试过调试代码了 这是视图方向代码: private GoogleMap mMap; FusedLocationProviderClient fusedLocationProviderClient; LocationCal

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

在这篇文章中,我从附近的地方(谷歌)检索纬度和经度。到目前为止一切都很好,但我不知道方向。它在地图上显示了布特点,但那个里什么也并没有发生

我检查了控制台,没有错误。我在谷歌上寻找了一个解决方案,但到目前为止我什么也没找到。我已经试过调试代码了

这是
视图方向
代码:

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();
    }
}
私有谷歌地图mMap;
FusedLocationProviderClient FusedLocationProviderClient;
LocationCallback LocationCallback;
位置请求位置请求;
位置mLastLocation;
标记mCurrentMarker;
多段线;
igoogservice;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u方向);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
mService=Common.getGoogleAppIserviceScalars();
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此);
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest、locationCallback、Looper.myLooper());
}
@凌驾
受保护的void onStop(){
fusedLocationProviderClient.RemovelocationUpdate(locationCallback);
super.onStop();
}
私有void buildLocationRequest(){
locationRequest=新的locationRequest();
位置请求设置间隔(1000);
locationRequest.SetFastTestInterval(1000);
位置请求。设置最小位移(10f);
locationRequest.setPriority(locationRequest.PRIORITY\u BALANCED\u POWER\u Accurance);
}
私有void buildLocationCallBack(){
locationCallback=新locationCallback(){
@凌驾
public void onLocationResult(LocationResult LocationResult){
super.onLocationResult(定位结果);
mLastLocation=locationResult.getLastLocation();
MarkerOptions MarkerOptions=新MarkerOptions()
.position(新车床(mLastLocation.getLatitude(),mLastLocation.getLength())
.title(“您的职位”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrentMarker=mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(newLatLng(mLastLocation.getLatitude(),mLastLocation.getLatitude()));
mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));
//创建标记目的地
LatLng destinationLatLng=新LatLng(Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLat()),
parseDouble(Common.currentResult.getGeometry().getLocation().getLng());
mMap.addMarker(新标记选项()
.位置(目标地图)
.title(Common.currentResult.getName())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
绘图路径(mLastLocation,Common.currentResult.getGeometry().getLocation());
}
};
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.getUiSettings().setZoomControlsEnabled(true);
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时的公共无效(位置){
mLastLocation=位置;
MarkerOptions MarkerOptions=新MarkerOptions()
.position(新车床(mLastLocation.getLatitude(),mLastLocation.getLength())
.title(“您的职位”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrentMarker=mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(newLatLng(mLastLocation.getLatitude(),mLastLocation.getLatitude()));
mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));
//创建标记目的地
LatLng destinationLatLng=新LatLng(Double.parseDouble(Common.currentResult.getGeometry().getLocation().getLat()),
parseDouble(Common.currentResult.getGeometry().getLocation().getLng());
mMap.addMarker(新标记选项()
.位置(目标地图)
.title(Common.currentResult.getName())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
绘图路径(mLastLocation,Common.currentResult.getGeometry().getLocation());
}
});
}
专用void绘图路径(位置mLastLocation,com.example.osim4.travelonbucovina.Model.Location){
//清除所有多段线
如果(多段线!=null)
polyline.remove();
字符串原点=新的StringBuilder(String.valueOf(mLastLocation.getLatitude()).append(“,”).append(String.valueOf(mLastLocation.getLatitude()))
.toString();
字符串目标=新建StringBuilder(location.getLat()).append(“,”).append(location.getLn