package se.peterbjorkman.android.trafikkamera;
import java.util.List;
import se.peterbjorkman.android.trafikkamera.CameraMapOverlay.OnTapListener;
import se.peterbjorkman.android.trafikkamera.providers.CameraContentProvider;
import se.peterbjorkman.android.trafikkamera.providers.CameraDefs;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
public class CameraMapActivity extends MapActivity {
private MyLocationOverlay mMyLocationOverlay;
private CameraMapOverlay mItemizedoverlay;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mItemizedoverlay = new CameraMapOverlay(drawable);
addCameraMarkers();
mMyLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);
mapOverlays.add(mItemizedoverlay);
mapOverlays.add(mMyLocationOverlay);
mItemizedoverlay.setOnTapListener(new OnTapListener() {
@Override
public void onTap(CameraOverlayItem item) {
long id = item.getId();
Context context = CameraMapActivity.this;
Intent launchIntent = new Intent(context,
CameraPictureActivity.class);
launchIntent.putExtra(CameraPictureActivity.EXTRA_ID, id);
context.startActivity(launchIntent);
}
});
}
@Override
protected void onResume(){
super.onResume();
mMyLocationOverlay.enableCompass();
mMyLocationOverlay.enableMyLocation();
}
@Override
protected void onPause(){
super.onPause();
mMyLocationOverlay.disableCompass();
mMyLocationOverlay.disableMyLocation();
}
private void addCameraMarkers() {
CameraMapOverlay itemizedoverlay = mItemizedoverlay;
itemizedoverlay.clearOverlayItems();
Cursor cursor = getCursor();
Resources resources = getResources();
while (cursor.moveToNext()){
Double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow(CameraDefs.LONGITUDE));
Double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow(CameraDefs.LATITUDE));
long id = cursor.getLong(cursor.getColumnIndexOrThrow(CameraDefs.ID));
addCamera(itemizedoverlay, resources, longitude, latitude, id);
}
cursor.close();
}
private Cursor getCursor() {
String[] projection = new String[] {
CameraDefs.LONGITUDE,
CameraDefs.LATITUDE,
CameraDefs.ID };
String filter = CameraDefs.LATITUDE + " IS NOT NULL AND " + CameraDefs.LONGITUDE + " IS NOT NULL";
return managedQuery(
CameraContentProvider.CONTENT_URI,
projection,
filter,
null,
null);
}
private void addCamera(CameraMapOverlay itemizedoverlay,
Resources resources, Double longitude, Double latitude, long id) {
Drawable icon = resources.getDrawable(R.drawable.icon);
CameraOverlayItem overlayitem = new CameraOverlayItem(id, longitude, latitude, icon);
itemizedoverlay.addOverlayItem(overlayitem);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
|