如何通过选择具有地点自动完成功能的标记来绘制路线?

时间:2019-04-02 10:56:39

标签: android google-places-api google-maps-direction-api

当我从“地方AutoComplete”搜索栏中放置目的地标记时,我想在地图上绘制路线。从“自动完成”中选择放下位置后,便需要该路线。我已经看到了一些绘制路径的示例,但无法通过地方Autocomplete找到任何示例。我的地图和地方Autocomplete正常工作。我该怎么办?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "my_api_ke");
    }


    /*Map Initialisation*/
    mv = findViewById(R.id.Gmapview);
    mv.onCreate(savedInstanceState);
    mv.getMapAsync(this);


    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = lm.getBestProvider(new Criteria(), false);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    checkLocationPermission();
    enableMyLocationIfPermitted();


    //Initialize AutoCompleteSupportFragment

    //Pickup Search Bar
    supportFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.pickup);


    supportFragment1 = (AutocompleteSupportFragment) getSupportFragmentManager()
            .findFragmentById(R.id.drop);

    //Initialize the Fields to get Latitude and Langitude and name of the selected place.
    supportFragment.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME));
    supportFragment1.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME));

    //Placing Pickup Marker
    supportFragment.setOnPlaceSelectedListener(newPlaceSelectionListener(){
        @Override
        public void onPlaceSelected(@NonNull Place pickup) {

            Log.i("PickUP", "Place: " + pickup.getName() + ", ");
            String name = pickup.getName();
            LatLng latLng = pickup.getLatLng();

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(name);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gm.addMarker(markerOptions);

            gm.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));

        }


        @Override
        public void onError(@NonNull Status status) {
            Log.i("pickup", "An error occurred: " + status);

        }
    });

    //Placing Drop Marker
    supportFragment1.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place drop) {
            Log.i("Drop", "Place: " + drop.getName() + ", ");

            String name = drop.getName();
            LatLng latLng = drop.getLatLng();

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gm.addMarker(markerOptions);

            gm.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));

        }

        @Override
        public void onError(@NonNull Status status) {
        Log.i("drop", "An error occurred: " + status);

        }
    });
}

2 个答案:

答案 0 :(得分:0)

假设您的LatLng对象正确,并且只需要绘制路线,这是您应该做的事情

创建一个名为DirectionsJSONParser的Helper类,该类以后将对折线进行解码,并为您提供一个经度对象列表。

这是该课程DirectionsJSONParser

的链接

对于RetrofitClient,这些是进口商品,请确保您将其添加

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; 

将此添加到您的build.gradle(app)文件

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

创建改造客户端

//this is a singleton of Retrofit
public class RetrofitClient {
private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if(retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
 //return retrofit object
    return retrofit;
 }
}

接下来创建一个Google API接口

public interface IGoogleAPI {

@GET
Call<String> getPath(@Url String url);
}

创建一个通用类

public class Common {
   public static String baseURL = "https://maps.googleapis.com/";
   public static IGoogleAPI getGoogleAPI() {
        return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
    }
}

最后但并非最不重要的一点是,在您的活动中声明一个变量

IGoogleAPI mService;

在onCreate中初始化该变量

mService = Common.getGoogleAPI();

创建绘制路线的方法

private void getDirection() {

  String requestAPI;
    try {

        requestAPI = "https://maps.googleapis.com/maps/api/directions/json?" +
                "mode=driving&" +
                "transit_routing_preference=less_driving&" +
                "origin=" + pickupMarker.getLatitude() + "," + pickupMarker.getLongitude() +
                "&destination=" + dropMarker.getLatitude() + "," + dropMarker.getLongitude() + "&" +
                "key=" + getResources().getString(R.string.google_direction_apis);


        mService.getPath(requestAPI)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, retrofit2.Response<String> response) {
                        try {
                            new ParserTask().execute(response.body().toString());

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

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

                    }
                });


    } catch (Exception e) {
        e.printStackTrace();
    }


}

不要忘记

  1. 要在build.gradle中添加改造依赖项
  2. 要在requestString中更改您的API_KEY。

这是一个很长的代码,所以如果您有任何问题请告诉我。

获取放置位置后,调用此函数getDirection()。

编辑

private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    ProgressDialog mDialog = new ProgressDialog(NavigationTracking.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog.setMessage("Executing...");
        mDialog.show();
    }

    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;
        try {
            jObject = new JSONObject(strings[0]);
            DirectionJsonParser parser = new DirectionJsonParser();
            routes = parser.parse(jObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return routes;
    }

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

        ArrayList points = null;
        PolylineOptions polylineOptions = null;

        Toast.makeText(NavigationTracking.this, "" + lists.size(), Toast.LENGTH_SHORT).show();

        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 < path.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(10);
            polylineOptions.color(Color.RED);
            // the shortest line between two points
            polylineOptions.geodesic(true);
        }

        if (polylineOptions != null) {
            direction = mMap.addPolyline(polylineOptions);
        }


    }
}

声明

private Polyline direction;

并在致电getDirection()

之前添加此测试
if (direction != null)
    direction.remove();

编辑2

像这样在onCreate()之前声明2个LatLng变量:

LatLng pickupLoc;
LatLng dropLoc;

然后,当您选择放置位置时,代码将变成这样

//Placing Pickup Marker
supportFragment.setOnPlaceSelectedListener(newPlaceSelectionListener(){
    @Override
    public void onPlaceSelected(@NonNull Place pickup) {

        Log.i("PickUP", "Place: " + pickup.getName() + ", ");
        String name = pickup.getName();
        pickupLoc = pickup.getLatLng();

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(pickupLoc );
        markerOptions.title(name);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        gm.addMarker(markerOptions);

        gm.moveCamera(CameraUpdateFactory.newLatLngZoom(pickupLoc, 11));

    }


    @Override
    public void onError(@NonNull Status status) {
        Log.i("pickup", "An error occurred: " + status);

    }
});


//Placing Drop Marker
supportFragment1.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(@NonNull Place drop) {
        Log.i("Drop", "Place: " + drop.getName() + ", ");

        String name = drop.getName();
        dropLoc = drop.getLatLng();

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(dropLoc );
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        gm.addMarker(markerOptions);

        gm.moveCamera(CameraUpdateFactory.newLatLngZoom(dropLoc, 11));

        //here you test if your locations aren't null call getDirection() like this

        if (pickupLoc != null && dropLoc != null) {
            if (direction != null)
               direction.remove();

            getDirection();
        }

    }

    @Override
    public void onError(@NonNull Status status) {
    Log.i("drop", "An error occurred: " + status);

    }
});

答案 1 :(得分:0)

所以@Amine的答案基本上是正确的,但是我需要在这里做一些细微的改动:

private void getDirection() {

String requestAPI;
try {

    requestAPI = "https://maps.googleapis.com/maps/api/directions/json?" +
            "mode=driving&" +
            "transit_routing_preference=less_driving&" +
            "origin=" + pickuploc.latitude() + "," + 
             pickuploc.longitude() +
            "&destination=" + droploc.latitude() + "," + 
             droploc.longitude() + "&" +
            "key=" + getResources().getString(R.string.google_direction_apis);

             /*If you get "PolylineOptions cannot be null" error and app crashes 
             while trying to draw polyline then try pasting your API 
             key in string directly like this "key=somekey". Though only for testing. */

             mService.getPath(requestAPI)
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, retrofit2.Response<String> 
             response) {
                    try {
                        new ParserTask().execute(response.body().toString());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

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

                }
            });


               } catch (Exception e) {
                 e.printStackTrace();
}


}