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
| @SuppressLint("NewApi")
public class TestMap extends Activity implements LocationListener {
private GoogleMap map;
double latitudemin;
double longitudemin;
Marker marker;
ArrayList<HashMap<String, String>> centersList;
private static final String TAG_LONG = "longitude";
private static final String TAG_LAT = "latitude";
int indexMin;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all products list
private static String url_all_products = "http://xxx";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "centers";
private static final String TAG_PID = "_id";
private static final String TAG_NAME = "name";
private static final String TAG_TEL = "tel";
private static final String TAG_ADDRESS = "address";
private static final String TAG_FIELD = "field";
private static final String TAG_EMAIL = "email";
private static final String TAG_CITY = "city";
// products JSONArray
JSONArray centers = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.localisation);
// Hashmap for ListView
centersList = new ArrayList<HashMap<String, String>>();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
// Loading products in Background Thread
new LoadAllCenters().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.localisation, menu);
return true;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (map != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
private void changeTypeMap() {
//Normal map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Hibrid map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// satellite map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllCenters extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TestMap.this);
pDialog.setMessage("Loading Map View. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Centers: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
centers = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < centers.length(); i++) {
JSONObject c = centers.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String tel = c.getString(TAG_TEL);
String address = c.getString(TAG_ADDRESS);
String field = c.getString(TAG_FIELD);
String city = c.getString(TAG_CITY);
String email = c.getString(TAG_EMAIL);
double longitud = c.getDouble(TAG_LONG);
String longitude = Double.toString(longitud);
double lati = c.getDouble(TAG_LAT);
String latitude = Double.toString(lati);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_TEL, tel);
map.put(TAG_ADDRESS, address);
map.put(TAG_CITY, city);
map.put(TAG_FIELD, field);
map.put(TAG_EMAIL, email);
map.put(TAG_LONG, longitude);
map.put(TAG_LAT, latitude);
// adding HashList to ArrayList
centersList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// calcul distance min
int indexDistancemin = calculDistanceMin();
// Log.i(getClass().getName(), "index : " + indexmin);
String latitudemn = centersList.get(indexDistancemin).get(TAG_LAT);
double latitudemin = Double.parseDouble(latitudemn);
String longitudemn = centersList.get(indexDistancemin).get(TAG_LONG);
double longitudemin = Double.parseDouble(longitudemn);
Log.i(getClass().getName(), "latitude min : " + latitudemin + " longitude min: " + longitudemin);
// La nouvelle latLng celle de la latitude et longitude min
final LatLng latLng = new LatLng(latitudemin, longitudemin);
marker = map.addMarker(new MarkerOptions().title("center").position(new LatLng(latitudemin, longitudemin)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomIn());
}
}
public int calculDistanceMin(){
float[] results = new float[3];
//initialisation distance min = distance entre 1èr point de la liste et la localisation actuelle
String latitudeinit = centersList.get(1).get(TAG_LAT);
double latitudemin = Double.parseDouble(latitudeinit);
String longitudeinit = centersList.get(1).get(TAG_LONG);
double longitudemin = Double.parseDouble(longitudeinit);
Log.i(getClass().getName(), "latitude default : " + latitudemin + " longitude default: " + longitudemin);
Location.distanceBetween(map.getMyLocation().getLatitude(), map.getMyLocation().getLongitude(), microdegrees(latitudemin), microdegrees(longitudemin), results);
float distanceMin = results[0];
float dmin = 0;
indexMin = 0;
for (int i = 1 ; i< centersList.size() ; i++){
String latitudei = centersList.get(i).get(TAG_LAT);
double latitude = Double.parseDouble(latitudei);
String longitudei = centersList.get(i).get(TAG_LONG);
double longitude = Double.parseDouble(longitudei);
// GeoPoint point = new GeoPoint(microdegrees(latitudeElmt),microdegrees(longitudeElmt));
//myLocation = new MyLocationOverlay(getApplicationContext(), mapView);
Location.distanceBetween(map.getMyLocation().getLatitude(), map.getMyLocation().getLongitude(), microdegrees(latitude), microdegrees(longitude), results);
if (results[0] < distanceMin){
distanceMin = results[0];
indexMin = i;
}
}
dmin = results[0];
return indexMin;
}
private int microdegrees(double value){
return (int)(value*1000000);
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
} |
Partager