This class gets the users location and puts there poistion on the map.
Using java Syntax Highlighting
- public class userLocation extends MapActivity implements LocationListener {
- /** Called when the activity is first created. */
- EditText txted = null;
- Button btnSimple = null;
- MapView myMapView = null;
- MapController mc = null;
- Drawable defaultMarker = null;
- GeoPoint p = null;
- //String coordinates[] = {"51.150", "-2.592"};
- double latitude = 51.150, longitude = -2.592;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // Creating TextBox displying Lat, Long
- //String currentLocation = "Lat: " + latitude + " Lng: " + longitude;
- //txted.setText(currentLocation);
- // Creating and initializing Map
- myMapView = (MapView) findViewById(R.id.mapView);
- p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
- myMapView.setSatellite(true);
- mc = myMapView.getController();
- mc.setCenter(p);
- mc.setZoom(14);
- // Add a location mark
- MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
- List<Overlay> list = myMapView.getOverlays();
- list.add(myLocationOverlay);
- // Adding zoom controls to Map
- ZoomControls zoomControls = (ZoomControls) myMapView.getZoomControls();
- zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
- myMapView.addView(zoomControls);
- myMapView.displayZoomControls(true);
- // Getting locationManager and reflecting changes over map if distance travel by
- // user is greater than 500m from current location.
- LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
- }
- /* This method is called when use position will get changed */
- public void onLocationChanged(Location location) {
- if (location != null) {
- double latitude = location.getLatitude();
- double longitude = location.getLongitude();
- //String currentLocation = "Lat: " + lat + " Lng: " + lng;
- // txted.setText(currentLocation);
- p = new GeoPoint((int) latitude * 1000000, (int) longitude * 1000000);
- mc.animateTo(p);
- }
- }
- public void onProviderDisabled(String provider) {
- // required for interface, not used
- }
- public void onProviderEnabled(String provider) {
- // required for interface, not used
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- // required for interface, not used
- }
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- //zoom in and out
- class MyLocationOverlay extends com.google.android.maps.Overlay {
- @Override
- public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
- super.draw(canvas, mapView, shadow);
- Paint paint = new Paint();
- // Converts lat/lng-Point to OUR coordinates on the screen.
- Point myScreenCoords = new Point();
- mapView.getProjection().toPixels(p, myScreenCoords);
- paint.setStrokeWidth(1);
- paint.setARGB(255, 255, 255, 255);
- paint.setStyle(Paint.Style.STROKE);
- //canvas.drawBitmap(myScreenCoords.x, myScreenCoords.y, paint);
- canvas.drawText("Here I am...", myScreenCoords.x, myScreenCoords.y, paint);
- return true;
- } }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
This class draws my festival site overlay onto the map
Using java Syntax Highlighting
- public class map extends MapActivity {
- private MapView myMap;
- MapView gMapView = null;
- private MyLocationOverlay myLocOverlay;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
- String coordinates[] = {"51.150", "-2.592"};
- double lat = Double.parseDouble(coordinates[0]);
- double lng = Double.parseDouble(coordinates[1]);
- GeoPoint p = new GeoPoint(
- (int) (lat * 1E6),
- (int) (lng * 1E6));
- MapView map = (MapView) findViewById(R.id.mapView);
- MapController mc = map.getController();
- mc.animateTo(p);
- mc.setZoom(14);
- map.setBuiltInZoomControls(true);
- List<Overlay> overlays = map.getOverlays();
- overlays.add(new MyOverlay(this,p, R.drawable.icon));
- map.invalidate();
- }
- @Override
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- private void Menu (MenuItem MyLocation
- ){
- MyLocation = (MenuItem)findViewById(R.id.menu_ok);
- OnMenuItemClickListener menuItemClickListener = null;
- MyLocation.setOnMenuItemClickListener(menuItemClickListener) ;
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_ok:
- Intent intent = new Intent(map.this, userLocation.class);
- startActivity(intent);
- Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
- break;
- }
- return true;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- MenuInflater myMenuInflater = getMenuInflater();
- myMenuInflater.inflate(R.layout.menu, menu);
- return true;
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

