package com.dhec.MPOS;
import com.esri.android.map.LocationService;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.LinearUnit;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.geometry.Unit;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Mapscreen extends Activity {
 
 MapView map = null;
  
     // The circle area specified by search_radius and input lat/lon serves
  // searching purpose. It is also used to construct the extent which
  // map zooms to after the first GPS fix is retrieved.
  final static double SEARCH_RADIUS = 5;
  protected static final Intent OnStatusChangedListener = null;
 /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mapscreen);
  // Retrieve the map and initial extent from XML layout
  map = (MapView)findViewById(R.id.map);
    
  // Add dynamic layer to MapView
  map.addLayer(new ArcGISTiledMapServiceLayer("" +
  "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
  map.addLayer(new ArcGISDynamicMapServiceLayer("" +
     "http://TEST/ArcGIS/rest/services/Mobile/MPOS/MapServer"));
  
  //Button Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Mapscreen.this, R.raw.watersplash);
  
  //Setting up the button references
        Button ZoomToMyLocation = (Button) findViewById (R.id.button3);        
        
       //When clicked, Activates the Zoom To GPS process
        ZoomToMyLocation.setOnClickListener(new View.OnClickListener() {
   
         public void onClick(View v) {
       // TODO Auto-generated method stub
       buttonSound.start();
       
                       //I need the ZoomToMyLocation button to excute here I think?
       map.setOnStatusChangedListener(new OnStatusChangedListener(){
                        //Starts searching for the GPS location of the phone. Below here I've gotten this code to work before, but not
                        //with my custom button above. 
   private static final long serialVersionUID = 1L;
   public void onStatusChanged(Object source, STATUS status) {
    if (source == map && status == STATUS.INITIALIZED) {
     LocationService ls = map.getLocationService();
     ls.setAutoPan(false);
     ls.setLocationListener(new LocationListener() {
      boolean locationChanged = false;
      // Zooms to the current location when first GPS fix
      // arrives.
      public void onLocationChanged(Location loc) {
       if (!locationChanged) {
        locationChanged = true;
        double locy = loc.getLatitude();
        double locx = loc.getLongitude();
        Point wgspoint = new Point(locx, locy);
        Point mapPoint = (Point) GeometryEngine
          .project(wgspoint,
            SpatialReference.create(4326),
            map.getSpatialReference());
        Unit mapUnit = map.getSpatialReference()
          .getUnit();
        double zoomWidth = Unit.convertUnits(
          SEARCH_RADIUS,
          Unit.create(LinearUnit.Code.MILE_US),
          mapUnit);
        Envelope zoomExtent = new Envelope(mapPoint,
          zoomWidth, zoomWidth);
        map.setExtent(zoomExtent);
       }
      }
      public void onProviderDisabled(String arg0) {
      }
      public void onProviderEnabled(String arg0) {
      }
      public void onStatusChanged(String arg0, int arg1,
        Bundle arg2) {
      }
     });
     ls.start();
    }
   }
  });
    
    }});
 }
 protected void onPause() {
  super.onPause();
  map.pause();
 }
 protected void onResume() {
  super.onResume();
  map.unpause();
 }
}
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		package com.dhec.MPOS;
import com.esri.android.map.LocationService;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.LinearUnit;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.geometry.Unit;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Mapscreen extends Activity {
 
  // The circle area specified by search_radius and input lat/lon serves
  // searching purpose. It is also used to construct the extent which
  // map zooms to after the first GPS fix is retrieved.
  final static double SEARCH_RADIUS = 5;
 
  MapView map = null;
  Button ZoomToMyLocation;
    /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   setContentView(R.layout.mapscreen);
    // Retrieve the map and initial extent from XML layout
    map = (MapView) findViewById (R.id.map);
      
    // This adds ESRI's titled map service layer "World Street Map" to the MapView, as well as my 
    // dynamics map service "MPOS"
    map.addLayer(new ArcGISTiledMapServiceLayer("" +
    "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
    map.addLayer(new ArcGISDynamicMapServiceLayer("" +
    "http://TEST/ArcGIS/rest/services/Mobile/MPOS/MapServer"));
  
//Setting up the button reference
          ZoomToMyLocation = (Button) findViewById (R.id.button1);
              
          //When "Button ZoomToMyLocation" is clicked, method "public void onClick(View v)" sets up for location listening. 
          ZoomToMyLocation.setOnClickListener(new View.OnClickListener() {
     
            public void onClick(View v) {
          LocationService ls = map.getLocationService();
     ls = map.getLocationService();
     ls.setAccuracyCircleOn(true);
     ls.setAutoPan(true);
       ls.setLocationListener(new LocationListener() {
      boolean locationChanged = false;
        // Zooms to the current location when first GPS fix arrives.
     public void onLocationChanged(Location loc) {
      if (!locationChanged) {
       locationChanged = true;
       double locy = loc.getLatitude();
       double locx = loc.getLongitude();
       Point wgspoint = new Point(locx, locy);
       Point mapPoint = (Point) GeometryEngine
         .project(wgspoint,
           SpatialReference.create(4326),
           map.getSpatialReference());
       Unit mapUnit = map.getSpatialReference()
         .getUnit();
       double zoomWidth = Unit.convertUnits(
         SEARCH_RADIUS,
         Unit.create(LinearUnit.Code.MILE_US),
         mapUnit);
       Envelope zoomExtent = new Envelope(mapPoint,
         zoomWidth, zoomWidth);
       map.setExtent(zoomExtent);
      }
     }
     public void onProviderDisabled(String arg0) {
     }
     public void onProviderEnabled(String arg0) {
     }
     public void onStatusChanged(String arg0, int arg1,
       Bundle arg2) {
     }
    });    
     
           if (ls.isStarted() == false){
          ls.start();
           }
     
           else {
          ls.stop();
           };      
   
         }
          
          });
    } 
}
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		@Override
public void onStatusChanged(Object source, STATUS status) {
  if (OnStatusChangedListener.STATUS.INITIALIZED == status && source == _mapView) {
   _isMapLoaded = true;
  setMapType(_maptype);
  
  
  if(_center_lat == null || _center_lon == null){
   latlon = new Point(_DEFAULT_LON,_DEFAULT_LAT);
  }
  else{
   latlon = new Point(_center_lon,_center_lat);
  }
  Point point = (Point)GeometryEngine.project(latlon,SpatialReference.create(4326), _mapView.getSpatialReference());  
  _mapView.centerAt(point,false);
  
  if(_center_scale == null){
   _mapView.setScale(_DEFAULT_SCALE);
  }
  else{
   _mapView.setScale(_center_scale);
  }     
   
  Log.d("MapViewController", "setOnStatusChangedListener() STATUS: " + status);  
  dispatchStatusEvent(new MapViewControllerEvent(MapViewEvent.STATUS_INITIALIZED), status.toString()); //custom event            
   
  }
  if (OnStatusChangedListener.STATUS.LAYER_LOADING_FAILED == status && source == _mapView){
   Log.d("MapViewController", "setMapListeners() STATUS: " + status + ", " + status.toString());
   displayToast("There was a map problem with loading a layer: " + status.toString());
   dispatchStatusErrorEvent(new MapViewControllerEvent(MapViewEvent.STATUS_LAYER_LOADING_FAILED), status.toString()); //custom event
  }
}
