1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
package com.paad.earthquake;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class CopyOfEarthquakeMapFragment extends Fragment {
GoogleMap mapView;
Context context;
ArrayList<LatLng> arll = new ArrayList<LatLng>();
ArrayList<Float> armag = new ArrayList<Float>();
ArrayList<String> ardet = new ArrayList<String>();
ArrayList<String> ardateaff = new ArrayList<String>();
Cursor cur;
ArrayList<Marker> allMarkers = new ArrayList<Marker>();
String titre = "";
String snippet = "";
Handler handler;
Runnable myRunnable;
int i;
int taille;
long DELAY = 3000;
//Float zoom = 4.5f;
Float zoom = 7f;
Marker marker;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Lister();
refreshQuakeLocations();
mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();
mapView.setMapType(mapView.MAP_TYPE_HYBRID);
mapView.getUiSettings().setZoomControlsEnabled(true);
mapView.getUiSettings().setCompassEnabled(true);
mapView.getUiSettings().setMyLocationButtonEnabled(false);
mapView.getUiSettings().setRotateGesturesEnabled(true);
mapView.getUiSettings().setScrollGesturesEnabled(true);
mapView.getUiSettings().setTiltGesturesEnabled(true);
mapView.getUiSettings().setZoomGesturesEnabled(true);
mapView.setMyLocationEnabled(false);
i=0;
taille = arll.size();
useHandler();
// Associer une info au marker
mapView.setInfoWindowAdapter(new InfoWindowAdapter() {
public View getInfoWindow(Marker arg0) {
return null;
}
public View getInfoContents(Marker marker) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View myContentsView = inflater.inflate(R.layout.infocontent, null);
TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
return myContentsView;
}
});
}
public void Lister() {
String[] projection = new String[] {
EarthquakeProvider.KEY_ID,
EarthquakeProvider.KEY_DETAILS,
EarthquakeProvider.KEY_DATEAFF,
EarthquakeProvider.KEY_LOCATION_LAT,
EarthquakeProvider.KEY_LOCATION_LNG,
EarthquakeProvider.KEY_MAGNITUDE,
};
Earthquake earthquakeActivity = (Earthquake)getActivity();
String where = EarthquakeProvider.KEY_MAGNITUDE + " > " +
earthquakeActivity.minimumMagnitude;
ContentResolver cr = getActivity().getContentResolver();
cur = cr.query(EarthquakeProvider.CONTENT_URI, projection, where, null, null);
}
public void refreshQuakeLocations() {
arll.clear();
if (cur != null && cur.moveToFirst())
do {
int detIndex = cur.getColumnIndexOrThrow(EarthquakeProvider.KEY_DETAILS);
int dateaffIndex = cur.getColumnIndexOrThrow(EarthquakeProvider.KEY_DATEAFF);
int latIndex = cur.getColumnIndexOrThrow(EarthquakeProvider.KEY_LOCATION_LAT);
int lngIndex = cur.getColumnIndexOrThrow(EarthquakeProvider.KEY_LOCATION_LNG);
int magIndex = cur.getColumnIndexOrThrow(EarthquakeProvider.KEY_MAGNITUDE);
String details = cur.getString(detIndex);
ardet.add(details);
String dateaff = cur.getString(dateaffIndex);
ardateaff.add(dateaff);
Double lat = cur.getDouble(latIndex);
Double lng = cur.getDouble(lngIndex);
LatLng geoPoint = new LatLng(lat,lng);
arll.add(geoPoint);
Float magnitude = cur.getFloat(magIndex);
armag.add(magnitude);
} while(cur.moveToNext());
}
public void ajouterMarker(LatLng laln,Float m,String title,String snippet,Float zoomajouter){
Bitmap bm = marqueur(m);
marker = mapView.addMarker(new MarkerOptions()
.position(laln)
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory.fromBitmap(bm)));
allMarkers.add(marker);
//ICI animation
CameraPosition myPosition = new CameraPosition.Builder()
.target(laln).zoom(zoomajouter).bearing(0).tilt(30).build();
mapView.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
marker.showInfoWindow();
}
public void ajouterMarker_2(LatLng laln_2,Float zoomajouter_2){
marker = mapView.addMarker(new MarkerOptions()
.position(laln_2)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pixelblanc)));
CameraPosition myPosition_2 = new CameraPosition.Builder()
.target(laln_2).zoom(zoomajouter_2).bearing(0).tilt(30).build();
mapView.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition_2));
}
public void useHandler() {
handler = new Handler();
myRunnable = new Runnable(){
@Override
public void run() {
if(i < taille) {
LatLng laln_h = arll.get(i);
Float m_h = armag.get(i);
String title_h = ardet.get(i);
String snippet_h = ardateaff.get(i);
ajouterMarker(laln_h,m_h,title_h,snippet_h,zoom);
useHandler();
}
else if(i == taille) {
LatLng finale = new LatLng(51.150002,-110.199997);
zoom = 2.0f;
ajouterMarker_2(finale,zoom);
useHandler();
}
else{
handler.removeCallbacks(myRunnable);
return;
}
++i;
}
};
handler.postDelayed(myRunnable,DELAY);
}
public Bitmap marqueur(Float m){
Drawable shape = getResources().getDrawable(R.drawable.markergreen);
int px = getResources().getDimensionPixelSize(R.dimen.marker_size);
Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
if(m<3){
shape = getResources().getDrawable(R.drawable.markergreen);
}
if( m==3){
shape = getResources().getDrawable(R.drawable.markerorange);
}
if( m>3 && m<4){
shape = getResources().getDrawable(R.drawable.markerorange);
}
if(m>4){
shape = getResources().getDrawable(R.drawable.markerred);
}
shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
shape.draw(canvas);
Paint paint = new Paint();
paint.setTextSize(50);
paint.setColor(Color.WHITE);
canvas.drawText(m.toString(),40,90,paint);
return mDotMarkerBitmap;
}
@Override
public void onResume() {
//mapView.onResume();
super.onResume();
}
@Override
public void onStop() {
super.onStop();
//mapView.onDestroy();
}
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.myMapView);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
} |
Partager