Ich habe eine Funktion geoLocate()
, die einen vom Benutzer eingegebenen Ort findet. Ich möchte, dass diese Funktion einen Ort in der Nähe des aktuellen Standorts des Benutzers findet. Kann jemand helfen?In der Nähe befindliche Orte finden
Wenn ich geoLocate("McDonald's")
anrufen, möchte ich, dass es den nächsten McDonald's relativ zum Standort des Benutzers findet.
public void geoLocate(String searchString) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(searchString, 3);
if (list.size() > 0) {
android.location.Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, "Found: " + locality, Toast.LENGTH_SHORT).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, 17);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions().title(locality).position(new LatLng(lat, lng));
marker = mMap.addMarker(options);
} else {
Toast.makeText(this, "No results found for: " + searchString, Toast.LENGTH_LONG).show();
}
}
Finden aktuellen Standort als Referenz:
public void setCurrentLocation() {
try {
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
if (currentLocation == null) {
Toast.makeText(this, "Couldn't connect!", Toast.LENGTH_SHORT).show();
} else {
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(update);
if (myLocationMarker != null) {
myLocationMarker.remove();
}
MarkerOptions options = new MarkerOptions().title(currentLocation.getLatitude() +
", " + currentLocation.getLongitude()).position(latLng);
myLocationMarker = mMap.addMarker(options);
}
} catch (SecurityException e) {
e.printStackTrace();
Toast.makeText(this, "My Location not enabled!", Toast.LENGTH_SHORT).show();
}
}