Ich habe seit Tage auf dieses Grübeln und Web-Surfen, aber nach etwa Produktivität für eine Woche habe ich beschlossen, diese vage Frage zu stellen. Meine Frage - Kann mir bitte jemand sagen, wie ich Anweisungen in mein Android-Projekt umsetzen kann?brauchen dringend Richtungen android Projekt hinzuzufügen
Um zu präzisieren, die genaue Funktion, für die ich hier frage, wird im nächsten Absatz hervorgehoben.
Das Ziel für mein Projekt: Immer wenn ein Benutzer einen Ort eingibt (er wird die Tastatur verstecken, nachdem er go geklickt hat), wird er dorthin gehen und dort einen Marker setzen. Dann wird die Route mit der kürzesten Reisezeit zwischen dem aktuellen Standort und dem Marker angezeigt. Ich weiß nicht, wie ich das Land begrenzen kann, in dem der Benutzer nach Orten suchen kann, aber ich bin mir ziemlich sicher, dass das einfach sein wird. Ich habe schon ein paar Teile meines Endziels, aber nicht wirklich, was ich brauche.
Schließlich ist dies meine MainActivity. Ich habe ein paar Klassen verwiesen ich hier nicht so hinzugefügt haben, einfach fragen, ob Sie sie benötigen diesen Code in Ihren eigenen Compiler zu laufen oder auf andere Weise:
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) {
}
}
das wurde juuuuust gefragt: http://stackoverflow.com/questions/38400355/google-directions-api-implementation-on-android/38400815 – mastrgamr
@mastrgamr wow! Als ich die Frage eintippte tauchte es nicht auf, und ich konnte das nicht früher finden (offensichtlich, weil es vor einer Stunde passiert ist). Vielen Dank! –