[英]I want to add marker from arrayList on google map in the interval of 2 seconds
Actually I am getting the latitude
and longitude
value from the ArrayList
, and I want to show these location on map in the interval of 3 seconds. 实际上,我是从
ArrayList
获取latitude
和longitude
值的,我想在3秒的间隔内在地图上显示这些位置。
I have created a method named waitSec()
and I'm calling it in displayLocation()
but this is not working because this blocked the main thread so I used handler
and asynctask
and I found difficulties in this. 我创建了一个名为
waitSec()
的方法,并在displayLocation()
调用了该方法,但是由于该方法阻塞了主线程,因此无法正常工作,因此我使用了handler
和asynctask
,但在此过程中发现了困难。
public void waitSec(){
long start = System.currentTimeMillis();
while(System.currentTimeMillis()<start+3000);
}
class MyAsyncTask extends AsyncTask<Integer,Integer,Void>{
@Override
protected Void doInBackground(Integer... integers) {
waitSec();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.e("TAG", "onPostExecute: we have waited 3 seconds" );
displayCurrentLocation();
}
}
private void displayCurrentLocation() {
for(int i=0;i<trackObjectList.size();i++){
//Log.e("TAG", "displayCurrentLocation: "+trackObjectList.get(i).getLatitude() );
if(trackObjectList.size()>0)
latLng = new LatLng(Double.parseDouble(trackObjectList.get(i).getLatitude())
,Double.parseDouble(trackObjectList.get(i).getLongitude()));
mMap.addMarker(new MarkerOptions().position(latLng)
.title("Here")
.icon(BitmapDescriptorFactory.fromBitmap(getSmallerSize(R.drawable.green_dot_th))));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15f));
//waitSec();
}
}
I expected that after every 2-3 seconds the markers will add on google map but it is not adding in this interval. 我希望每隔2-3秒,标记就会添加到Google地图上,但不会在此间隔内添加。 It shows all the latlng marks on google map.
它会在Google地图上显示所有纬度标记。
You can easily achieve this using RxJava 您可以使用RxJava轻松实现
Observable.from(listOfItems)
.zipWith(Observable.interval(2, TimeUnit.SECONDS), (item, notUsed) -> item)
.subscribe(
// add marker addition logic here
);
You need to animate your marker when create-- 创建时需要为标记制作动画-
private void animateMarker(final Marker marker) {
final Handler handler = new Handler();
final long startTime = SystemClock.uptimeMillis();
final long duration = 300; // ms
Projection proj = mMap.getProjection();
final LatLng markerLatLng = marker.getPosition();
Point startPoint = proj.toScreenLocation(markerLatLng);
startPoint.offset(0, -10);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - startTime;
float t = interpolator.getInterpolation((float) elapsed / duration);
double lng = t * markerLatLng.longitude + (1 - t) * startLatLng.longitude;
double lat = t * markerLatLng.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later (60fps)
handler.postDelayed(this, 16);
}
}
});
}
you have a for loop in displayCurrentLocation which is displaying everything everytime the asyncTask.onPostExecute() runs. 您在displayCurrentLocation中有一个for循环,该循环在每次asyncTask.onPostExecute()运行时显示所有内容。
try the following: 尝试以下方法:
private int objectsSize = trackObjectList.size()-1;//initialise this somewhere appropriate
private void displayCurrentLocation()
{
int i=objectsSize--;
//Log.e("TAG", "displayCurrentLocation: "+trackObjectList.get(i).getLatitude() );
if(i>=0)
{
latLng = new LatLng(Double.parseDouble(trackObjectList.get(i).getLatitude())
,Double.parseDouble(trackObjectList.get(i).getLongitude()));
mMap.addMarker(new MarkerOptions().position(latLng)
.title("Here")
.icon(BitmapDescriptorFactory.fromBitmap(getSmallerSize(R.drawable.green_dot_th))));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15f));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.