C'è un modo attraverso l'API delle mappe Android, dove posso rilevare il centro della mappa dopo che l'animazione pan è stata completata? Voglio usare queste informazioni per caricare i marker da un server in modo dinamico. Grazie BDandroid maps: come determinare il centro della mappa dopo aver completato il trascinamento
risposta
Si sta utilizzando un MapActivity
? Ecco il codice che ho usato:
MapView mapView = (MapView)findViewById(R.id.map);
Projection projection = mapView.getProjection();
int y = mapView.getHeight()/2;
int x = mapView.getWidth()/2;
GeoPoint geoPoint = projection.fromPixels(x, y);
double centerLatitude = (double)geoPoint.getLatitudeE6()/(double)1E6;
double centerLongitude = (double)geoPoint.getLongitudeE6()/(double)1E6;
si ha bisogno di aggiungere codice simile a questo anche:
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
boolean result = super.dispatchTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP)
reload_map_data(); /// call the first block of code here
return result;
}
Ho anche cercato una soluzione "è finita trascinare" che rileva la mappa centro al momento esattamente dopo che la mappa si è conclusa. Io non l'ho trovato, così ho fatto questa semplice implementazione che ha funzionato bene:
private class MyMapView extends MapView {
private GeoPoint lastMapCenter;
private boolean isTouchEnded;
private boolean isFirstComputeScroll;
public MyMapView(Context context, String apiKey) {
super(context, apiKey);
this.lastMapCenter = new GeoPoint(0, 0);
this.isTouchEnded = false;
this.isFirstComputeScroll = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
this.isTouchEnded = false;
else if (event.getAction() == MotionEvent.ACTION_UP)
this.isTouchEnded = true;
else if (event.getAction() == MotionEvent.ACTION_MOVE)
this.isFirstComputeScroll = true;
return super.onTouchEvent(event);
}
@Override
public void computeScroll() {
super.computeScroll();
if (this.isTouchEnded &&
this.lastMapCenter.equals(this.getMapCenter()) &&
this.isFirstComputeScroll) {
// here you use this.getMapCenter() (e.g. call onEndDrag method)
this.isFirstComputeScroll = false;
}
else
this.lastMapCenter = this.getMapCenter();
}
}
Questo è tutto, spero che aiuta! o/
Questo ha funzionato, questa dovrebbe essere la risposta accettata. – Federico
da August 2016 Maps per Android è in grado di rilevare eventi quali onCameraMoveStarted
(e le ragioni del movimento, ad esempio, REASON_GESTURE
, REASON_DEVELOPER_ANIMATION
.
Il seguente codice (per lo più ricavati dal docs) dà un idea di ciò che si può ottenere:?.
public class MyCameraActivity extends FragmentActivity implements
OnCameraMoveStartedListener,
OnCameraMoveListener,
OnCameraMoveCanceledListener,
OnCameraIdleListener,
OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_camera);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnCameraIdleListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnCameraMoveCanceledListener(this);
// Show Sydney on the map.
mMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
}
@Override
public void onCameraMoveStarted(int reason) {
if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
Toast.makeText(this, "The user gestured on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason == OnCameraMoveStartedListener
.REASON_API_ANIMATION) {
Toast.makeText(this, "The user tapped something on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason == OnCameraMoveStartedListener
.REASON_DEVELOPER_ANIMATION) {
Toast.makeText(this, "The app moved the camera.",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCameraMove() {
Toast.makeText(this, "The camera is moving.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraMoveCanceled() {
Toast.makeText(this, "Camera movement canceled.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraIdle() {
Toast.makeText(this, "The camera has stopped moving.",
Toast.LENGTH_SHORT).show();
// Here you get the camera center
GeoPoint geoPoint = projection.fromPixels(mMap.getHeight()/2, mMap.getWidth()/2);
double centerLat = (double)geoPoint.getLatitudeE6()/(double)1E6;
double centerLng = (double)geoPoint.getLongitudeE6()/(double)1E6;
}
}
questo gestire la "scappatella" in cui la mappa continua a muoversi un po 'modi se gettarlo (dopo si solleva il dito) –
Ciao grazie per la marcc risposta, ma Ho bisogno di trovare un modo per rilevare quando l'animazione pan si arresta in modo che Posso usare il tuo codice di calcolo centrale a quel punto. – vamsibm