迫切需要向 android 项目添加说明

Desperately need to add directions to android project

我已经为此思考和上网 ,但在 0 一周左右的生产力之后,我决定问这个模糊的问题。我的问题 - 谁能告诉我如何在我的 android 项目中实施指导?

为了详细说明,我在这里要求的确切功能在下一段中突出显示。

我的项目的目标:每当用户在一个位置键入内容(它会在单击“开始”后隐藏键盘),它会转到该位置并在那里放置一个标记。 然后它会显示当前位置和标记之间的最短旅行时间的路线。我不知道如何限制用户可以搜索位置的国家,但我很漂亮肯定会很容易。我已经有了我最终目标的点点滴滴,但不是我真正需要的。

最后这是我的 MainActivity。我已经引用了一些 类 我没有在这里添加所以,只需询问您是否需要它们 运行 在您自己的编译器中或其他地方使用此代码:

public class MainActivity extends FragmentActivity
        implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    @SuppressWarnings("unused")
    private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9002;
    @SuppressWarnings("unused")
    private static final String LOGTAG = "Maps";

    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    private static final float DEFAULTZOOM = 15;
    private GoogleApiClient mGoogleApiClient;
    private LocationListener mListener;
    private Marker marker;

    ArrayList<LatLng> mMarkerPoints;
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (servicesOK()) {
            setContentView(R.layout.activity_main);

            if (initMap()) {
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();
            } else {
                Toast.makeText(this, "Hmmm. Maps didn't load.", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "There's something wrong with your play services", Toast.LENGTH_SHORT).show();
        }

        UiSettings config = mMap.getUiSettings();
        config.setMapToolbarEnabled(false);
        config.setZoomControlsEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.legal:
                Intent intent = new Intent(this, LicenseActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MapStateManager mgr = new MapStateManager(this);
        CameraPosition position = mgr.getSavedCameraPosition();
        if (position != null) {
            CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
            mMap.moveCamera(update);
            mMap.setMapType(mgr.getSavedMapType());
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        MapStateManager mgr = new MapStateManager(this);
        mgr.saveMapState(mMap);
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    public boolean servicesOK() {
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        } else {
            Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private boolean initMap() {
        if (mMap == null) {
            MapFragment mapFrag =
                    (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }

    @SuppressWarnings("unused")
    private void gotoLocation(double lat, double lng) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
        mMap.moveCamera(update);
    }

    private void gotoLocation(double lat, double lng,
                              float zoom) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        mMap.moveCamera(update);
    }

    public void geoLocate(View v) throws IOException {

        EditText et = (EditText) findViewById(R.id.editText1);
        String location = et.getText().toString();
        if (location.length() == 0) {
            Toast.makeText(this, "Please enter a location", Toast.LENGTH_SHORT).show();
            return;
        }

        hideSoftKeyboard(v);

        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(location, 1);
        Address add = list.get(0);
        String locality = add.getLocality();
        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double lat = add.getLatitude();
        double lng = add.getLongitude();

        gotoLocation(lat, lng, DEFAULTZOOM);

        if (marker != null) {
            marker.remove();
        }

        MarkerOptions options = new MarkerOptions()
                .position(new LatLng(lat, lng));
        marker = mMap.addMarker(options);
    }

    private void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    public void showCurrentLocation(MenuItem item) {
        int permCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (permCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            Location currentlocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);
            if (currentlocation == null) {
                Toast.makeText(this, "Couldn't find you!", Toast.LENGTH_SHORT).show();
            } else {
                LatLng latlng = new LatLng(
                        currentlocation.getLatitude(),
                        currentlocation.getLongitude()
                );
                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
                        latlng, 15
                );
                mMap.animateCamera(update);
            }
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        int permCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (permCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            Toast.makeText(this, "Go go go!", Toast.LENGTH_SHORT).show();

            mListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    gotoLocation(location.getLatitude(), location.getLongitude(), 15);
                }
            };

            LocationRequest request = LocationRequest.create();
            request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            request.setInterval(20000);
            request.setFastestInterval(0);
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, mListener
            );
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

在 github 上查看此图书馆:

jd-alexander/Google-Directions-Android

我在我的项目中使用它,您可以克隆它并根据您的特定需求进行修改。

乍一看,您在问题中的逻辑似乎很合理,但为了限制搜索位置,我可能只接受搜索框中的任何查询并进行比较,看看它是否在指定范围内 LatLngBounds你提供。