Back to project page android_network_discovery.
The source code is released under:
GNU General Public License
If you think the Android project android_network_discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package info.lamatricexiste.network; /* w w w . ja va 2s .co m*/ import java.util.ArrayList; import java.util.List; import org.osmdroid.DefaultResourceProxyImpl; import org.osmdroid.ResourceProxy; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.ItemizedIconOverlay; import org.osmdroid.views.overlay.OverlayItem; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Point; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Toast; public class DiscoverActivity extends Activity implements LocationListener { private DefaultResourceProxyImpl defaultResourceProxyImpl; private MyItemizedIconOverlay myItemizedIconOverlay; private LocationManager locationManager; private String provider; public static float lat, lng; public static float speed, accuracy, bearing; public static double altitude; public static String providers; private GeoPoint point; private static MapView mapView; ArrayList<OverlayItem> overlayItemArray = new ArrayList<OverlayItem>(); private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; DatabaseHelper db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); db = new DatabaseHelper(getApplicationContext()); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); Location location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); provider = locationManager.getBestProvider(criteria, true); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); // Initialize the location fields if (location != null) { updateLoc(location); } mapView = new MapView(this, 256); // Constructor mapView.setClickable(true); // mapView.setBuiltInZoomControls(true); point = new GeoPoint(lat, lng); //mapView.getController().setZoom(20); // Initial Zoom Level mapView.getController().setCenter(point); mapView.setMultiTouchControls(true); mapView.setUseDataConnection(true); // True=>Use Data Connection Button btn = new Button(this); btn.setText("Next"); btn.setTextAppearance(getApplicationContext(), R.style.ButtonTop); btn.setBackgroundResource(R.drawable.arrow_right); btn.setBackgroundColor(getResources().getColor( android.R.color.background_dark)); btn.setTextColor(getResources().getColor(android.R.color.white)); final RelativeLayout relativeLayout = new RelativeLayout(this); final RelativeLayout.LayoutParams mapViewLayoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); final RelativeLayout.LayoutParams buttonLayoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); relativeLayout.addView(mapView, mapViewLayoutParams); relativeLayout.addView(btn, buttonLayoutParams); setContentView(relativeLayout); defaultResourceProxyImpl = new DefaultResourceProxyImpl(this); myItemizedIconOverlay = new MyItemizedIconOverlay(overlayItemArray, null, defaultResourceProxyImpl); mapView.getOverlays().add(myItemizedIconOverlay); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(DiscoverActivity.this, ActivityMain.class)); } }); } /* Request updates at startup */ @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this); } /* Remove the locationlistener updates when Activity is paused */ @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { speed = location.getSpeed(); providers = location.getProvider(); altitude = location.getAltitude(); accuracy = location.getAccuracy(); bearing = location.getBearing(); System.out.println(""+speed); updateLoc(location); } private void updateLoc(Location location) { lat = (float) (location.getLatitude()); lng = (float) (location.getLongitude()); setOverlayLoc(location); } private void setOverlayLoc(Location overlayloc) { GeoPoint overlocGeoPoint = new GeoPoint(overlayloc); OverlayItem newMyLocationItem = new OverlayItem("My Location", "My Location", overlocGeoPoint); overlayItemArray.clear(); overlayItemArray.add(newMyLocationItem); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onProviderDisabled(String provider) { Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show(); } private class MyItemizedIconOverlay extends ItemizedIconOverlay<OverlayItem> { public MyItemizedIconOverlay( List<OverlayItem> pList, org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener, ResourceProxy pResourceProxy) { super(pList, pOnItemGestureListener, pResourceProxy); } @Override public void draw(Canvas canvas, MapView mapview, boolean arg2) { super.draw(canvas, mapview, arg2); if (!overlayItemArray.isEmpty()) { // overlayItemArray have only ONE element so I hard code to // get(0) GeoPoint in = overlayItemArray.get(0).getPoint(); Point out = new Point(); mapview.getProjection().toPixels(in, out); } } @Override public boolean onSingleTapUp(MotionEvent event, MapView mapView) { return true; } } }