那里!我是一个全新的学习机器人和地理编码的人。我想知道如何显示当前地址而不是显示";给你"消息我知道我应该使用getFromLocation((方法来实现我的目标。然而,我不知道我该怎么做,该把它放在哪里
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private final int REQUEST_LOCATION_PERMISSIONS = 0;
private float mZoomLevel = 15;
private GoogleMap mMap;
private FusedLocationProviderClient mClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
// Create location request
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mClient = LocationServices.getFusedLocationProviderClient(this);
// Create location callback
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
for (Location location : locationResult.getLocations()) {
updateMap(location);
}
}
}
};
mClient = LocationServices.getFusedLocationProviderClient(this);
}
private void updateMap(Location location) {
// Get current location
LatLng myLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
// Place a marker at the current location
MarkerOptions myMarker = new MarkerOptions()
.title("Here you are!")
.position(myLatLng);
// Remove previous marker
mMap.clear();
// Add new marker
mMap.addMarker(myMarker);
// Move and zoom to current location at the street level
CameraUpdate update = CameraUpdateFactory.
newLatLngZoom(myLatLng, mZoomLevel);
mMap.animateCamera(update);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
CameraPosition cameraPosition = mMap.getCameraPosition();
mZoomLevel = cameraPosition.zoom;
}
});
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this,"Lat: " + marker.getPosition().latitude +
"nLong: " + marker.getPosition().longitude, Toast.LENGTH_LONG).show();
return false;
}
});
}
@Override
public void onPause() {
super.onPause();
mClient.removeLocationUpdates(mLocationCallback);
}
@SuppressLint("MissingPermission")
@Override
public void onResume() {
super.onResume();
if (hasLocationPermission()) {
mClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
}
}
private boolean hasLocationPermission() {
// Request fine location permission if not already granted
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this ,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
REQUEST_LOCATION_PERMISSIONS);
return false;
}
return true;
}
}
我试图将以下内容添加到代码中,以将消息更改到特定位置:
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
updateMap(location);
} catch (Exception e) {
e.printStackTrace();
}
}
getFromAddress方法返回可能地址的列表。您可以像下面的代码一样获得完整地址。
List<Address> fromLocation = geocoder.getFromLocation(location.latitude, location.longitude, 1);
Address address = fromLocation.get(0);
String zipCode = address.getPostalCode());
String city = address.getLocality();
String subAdmin = address.getSubAdminArea();
String countryCode = address.getCountryCode());
String state = address.getAdminArea());
你也可以更新你的标记如下
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.draggable(true);
markerOptions.position(latLng);
markerOptions.title(city);// your text put here
map.addMarker(markerOptions);