我是Android的新手。我正在使用许多硬编码的经纬度来安装android map.I已经将经纬度存储在arraylist中。有两个标记 - Bus和Student.I必须将Bus标记拖动到纬度每10秒后经度位置应该保持在最后位置,但我的标记没有更新到该位置。
public class TransportBusMapActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap map;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 100 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 10000; // 10 sec
LatLngBounds.Builder builder;
CameraUpdate cu;
int count=0;
ArrayList<LatLng> locations = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transport_bus_map);
locations.add(new LatLng(28.6311812, 77.3785497));
locations.add(new LatLng(28.6310870, 77.3796440));
locations.add(new LatLng(28.631012, 77.3805697));
locations.add(new LatLng(28.627462, 77.3838835));
locations.add(new LatLng(28.631107, 77.384071));
/**
* function to load map. If map is not created it will create it for you
**/
SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap) {
map=googleMap;
/**clear the map before redraw to them*/
map.clear();
/**Create dummy Markers List*/
List<Marker> markersList = new ArrayList<Marker>();
//if(locations!=null)
final Marker Bus = map.addMarker(new MarkerOptions().position(new LatLng(28.6311812, 77.3785497)).title("Bus"));
Marker Student = map.addMarker(new MarkerOptions().position(new LatLng(
28.631107, 77.384071)).title("Student"));
markersList.add(Bus);
final Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i <locations.size(); i++) {
LatLng currentPosition = locations.get(i);
Bus.setPosition(currentPosition);
CameraUpdate mLoc = CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(currentPosition).build());
map.moveCamera(mLoc);
h.postDelayed(this, MIN_TIME_BW_UPDATES);
}}
},MIN_TIME_BW_UPDATES);
markersList.add(Student);
/**create for loop for get the latLngbuilder from the marker list*/
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
/**initialize the padding for map boundary*/
int padding = 50;
/**create the bounds from latlngBuilder to set into map camera*/
LatLngBounds bounds = builder.build();
/**create the camera with bounds and padding to set into map*/
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
/**call the map call back to know map is loaded or not*/
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
/**set animated zoom camera into map*/
map.animateCamera(cu);
}
});
}