如何在Google Maps上显示当前位置?
那里!我是 android 和地理编码的全新学习者。我想知道如何显示当前地址而不是显示“你在这里”消息。我知道我应该使用 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();
}
}
there! I am a brand new learner of android and geocoding. And I wonder how I can display the current address rather than showing the "Here you are" message. I know I was supposed to use the getFromLocation() method to accomplish my goal. However, I do not know how I can do that and where to put it.
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;
}
}
I was trying to add the following to the code to change the message to a specific location:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getFromAddress方法返回可能地址的列表。您可以获取完整的地址,例如Bellow代码。
您也可以更新标记为波纹管
getFromAddress method return list of possible address.you can get full address like bellow code.
Also you can update your marker as bellow