gpt4 book ai didi

android - moveCamera 没有更新到我在 Google map Android 上的当前位置?

转载 作者:行者123 更新时间:2023-11-29 21:24:12 26 4
gpt4 key购买 nike

我有一个小问题,当我在手机上运行该应用程序时,我的 moveCamera 设置在我以前的位置(而不是我正在测试它的当前位置),我的 addCircle (设置在我以前的位置周围,而不是我现在的位置)。

这是我的代码:

public class MapDraw extends FragmentActivity {

static GoogleMap googleMap;
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g_maps_circle);

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);

circleDraw(latitude,longitude);
zoomIn(latitude,longitude);
}
}

public void circleDraw(double i, double ii) {

googleMap.addCircle(new CircleOptions()
.center(new LatLng(i, ii))
.radius(1000)
.strokeColor(Color.BLACK)
.strokeWidth(2)
.fillColor(Color.argb(50, 238, 116, 116)));
}

public void zoomIn(double Lat, double Long) {

CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(Lat,Long));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);

}
}

我希望 moveCamera 位于我当前的位置,我的圈子也位于我当前的位置。有什么帮助吗?谢谢

最佳答案

实现位置监听器。并在 onLocationChanged 上重绘圆圈

import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

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.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class MapDraw extends FragmentActivity implements LocationListener {

static GoogleMap googleMap;
LocationManager locationManager;
String provider;
LatLng myPosition;

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

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);

}
}

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

public void circleDraw(double i, double ii) {

googleMap.addCircle(new CircleOptions().center(new LatLng(i, ii))
.radius(1000).strokeColor(Color.BLACK).strokeWidth(2)
.fillColor(Color.argb(50, 238, 116, 116)));
}

public void zoomIn(double Lat, double Long) {

CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Lat,
Long));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);

}

@Override
public void onLocationChanged(Location location) {
googleMap.clear();// clean the map
Toast.makeText(this, "Location Changed", Toast.LENGTH_SHORT).show();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
myPosition = new LatLng(latitude, longitude);

circleDraw(latitude, longitude);
zoomIn(latitude, longitude);
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

}

关于android - moveCamera 没有更新到我在 Google map Android 上的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368339/

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