gpt4 book ai didi

android - 如何在 map 上显示更新的当前位置作为谷歌地图

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:01 25 4
gpt4 key购买 nike

我正在尝试在 map 上显示当前更新的位置。

当我放大 map 并更新位置时,它会缩小并显示位置。它不显示相同缩放位置的更新位置。当位置更新时,我再次在 map 上设置标记。我认为在 map 上设置标记有问题。请帮助我显示当前更新的位置 map 。

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

GoogleMap googleMa;
double latitude;
private GoogleApiClient mGoogleApiClient;
double longitude;
private Location mLastLocation = null;
private LocationRequest mLocationRequest;
String mPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
protected LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
2);

locationManager = (LocationManager) MainActivity.this
.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

if (checkPlayServices()) {

buildGoogleApiClient();
createLocationRequest();
displayLocation();

}
initailizeMap();
}

public void initailizeMap() {
if (googleMa == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
googleMa = googleMap;
Log.d("aaaaaa", " on map --->" + latitude + " " + longitude);
displayLocation();

}

public void displayLocation() {
try {
GPSTracker gps = new GPSTracker(MainActivity.this);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Latitude: " + latitude + " Longitude: " + longitude, Toast.LENGTH_LONG).show();
final LatLng loc = new LatLng(latitude, longitude);
Marker ham = googleMa.addMarker(new MarkerOptions().position(loc).title("This is Me").icon(BitmapDescriptorFactory.fromResource(R.drawable.greenpointer)));
googleMa.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
}
} catch (Exception e) {
}
}

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i("aaaaaaaa", "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}

@Override
public void onConnected(Bundle arg0) {
startLocationUpdates();
}

@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
Log.d("aaaaaaaa===>", "" + String.valueOf(location.getLatitude()) + "\n" + String.valueOf(location.getLongitude()));
Toast.makeText(getApplicationContext(), "Location changed!",
Toast.LENGTH_SHORT).show();
displayLocation();
}

private boolean checkPlayServices() {

GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();

int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);

if (resultCode != ConnectionResult.SUCCESS) {
if (googleApiAvailability.isUserResolvableError(resultCode)) {
googleApiAvailability.getErrorDialog(this, resultCode,
1000).show();
} else {
Toast.makeText(getApplicationContext(),
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}

protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();

mGoogleApiClient.connect();

LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(100);
mLocationRequest.setFastestInterval(500);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);

PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {

final Status status = locationSettingsResult.getStatus();

switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(MainActivity.this, 2000);

} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}

protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000); // 10 sec
mLocationRequest.setFastestInterval(5000); // 5 sec
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(10); // 10 meters
}

protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}

protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}

@Override
protected void onDestroy() {
super.onDestroy();
stopLocationUpdates();
}

最佳答案

onLocationChanged 触发后,您需要从 location 实例而不是 获取 latitudelongitude code>gps(我不知道 GPSTracker 是什么,没有代码,但它肯定不会给你更新的经纬度)

@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
// use latitude and longitude given by
// location.getLatitude(), location.getLongitude()
// for updated location marker
Log.d("aaaaaaaa===>", "" + location.getLatitude() + "\n" + location.getLongitude());
// displayLocation();

// to remove old markers
googleMa.clear();
final LatLng loc = new LatLng(location.getLongitude(), location.getLongitude());

Marker ham = googleMa.addMarker(new MarkerOptions().position(loc).title("This is Me").icon(BitmapDescriptorFactory.fromResource(R.drawable.greenpointer)));
googleMa.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
}

关于android - 如何在 map 上显示更新的当前位置作为谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47766630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com