Code example for LocationClient

Methods: disconnect, isConnected, removeLocationUpdates

0
    //public static final String PROPERTY_REG_ID = "registration_id"; 
    //String GCM_SENDER_ID = "68787639537"; 
     
    //helper functions 
    private void stopPeriodicUpdates() { 
    	mLocationClient.removeLocationUpdates(this);
    } 
     
    private void startPeriodicUpdates() { 
    	mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } 
     
    private float clamp (float val) {
    	if (val <= 0) {
            return 0; 
    	} 
    	if (val >= 90) {
            return (float) 90; 
    	} 
    	return val;
    } 
     
    public void changeFocus(Location location, float tilt, float azi) {
    	 
    	LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
         
        if (!this.isRotationViewEnabled) {
            /* 
              String msg = "Updated Location: " + 
              Double.toString(location.getLatitude()) + "," + 
              Double.toString(location.getLongitude()); 
              Toast.makeText(this, "Focusing...", Toast.LENGTH_SHORT).show(); 
              Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
            */ 
            // Move the camera instantly to Sydney with a zoom of 15. 
            CameraPosition cameraPosition1 = new CameraPosition.Builder()
                .target(latLng)
                .zoom(20)
                .bearing(90)
                .tilt(90)
                .build();
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition1));
            this.stopPeriodicUpdates();
        } else { 
            // Move the camera instantly to Sydney with a zoom of 15. 
            CameraPosition cameraPosition2 = new CameraPosition.Builder()
                .target(latLng)
                .zoom(14+5*(-tilt/90))
                .bearing(-azi)
                .tilt(clamp(-tilt))
                .build();
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition2));
            //this.stopPeriodicUpdates(); 
        } 
    } 
     
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        /* 
          prefs = getSharedPreferences(MapGuideView.class.getSimpleName(),  
          Context.MODE_PRIVATE); 
        */ 
        setContentView(R.layout.activity_map_guide_view);
        this.context = this;
        SharedPreferences data = getSharedPreferences(PREFS_NAME, 0);
        String s = data.getString("string1", "string not found");
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
        camera_change_init = false;
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.guideMap)).getMap();
            // Check if we were successful in obtaining the map. 
            if (mMap != null) {
                // The Map is verified. It is now safe to manipulate the map. 
            	Log.d("MapViewGuide","MapRetrieved!");
            	 
            	// Connect to new location client instance 
            	mLocationClient = new LocationClient(this,this,this);
            	// Create the LocationRequest object 
                mLocationRequest = LocationRequest.create();
                // Use high accuracy 
                mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                // Set the update interval to 5 seconds 
                mLocationRequest.setInterval(UPDATE_INTERVAL);
                // Set the fastest update interval to 1 second 
                mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
                 
                // Connect to new location client instance 
            	mLocationClient = new LocationClient(this,this,this);
            	 
            	Toast.makeText(this, "Initializing", Toast.LENGTH_SHORT).show();
            	 
            	//initialize sensor manager 
            	mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
            	rotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
            	mSensorManager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
            	 
            	//Camera Listener on the Map view 
            	mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
 
                        @Override 
                        public void onCameraChange(CameraPosition position) {
                            mCamPos = position;
                        } 
                    }); 
            	 
            	//Parse for real time messaging 
            	Messenger = new ParseManager(this);
            	 
            	 
            } 
        } 
    } 
     
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.map_guide_view, menu);
        return true; 
    } 
 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.back_to_home) {
            Intent intent = new Intent(this, GuideMain.class);
            startActivity(intent);
            finish();
            return true; 
        } else if (item.getItemId() == R.id.tour_guide) {
            isRotationViewEnabled = false;
        		           	 
            Marker markStart = mMap.addMarker(new MarkerOptions()
                                              .position(new LatLng(this.mCurrentLocation.getLatitude(), this.mCurrentLocation.getLongitude())) // Mountain View
                                              .title("I am here!")
                                              .snippet("Population: Happiness"));
        		          	 
            //changeFocus(location, 0,0); 
            mapAnim anim = new mapAnim(mMap);
        		           	 
            Marker markEnd = mMap.addMarker(new MarkerOptions()
                                            .position(new LatLng(34.0431, -118.2671)) // Staples Center
                                            .title("I am here now!")
                                            .snippet("Population: Happiness"));
        		           	 
            anim = new mapAnim(mMap);
            anim.animateTo(markStart, markEnd);
            return true; 
        } 
        return false; 
    } 
 
    @Override 
    protected void onStart() { 
        super.onStart(); 
        // Connect the client. 
        Toast.makeText(this, "Starting Activity...", Toast.LENGTH_SHORT).show();
        mLocationClient.connect();
        final Button deButton = (Button) findViewById(R.id.debug);
        deButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(context, 
                                   "Azimuth:"+Float.toString(mAzi)+","+"Tilt:"+Float.toString(mTilt), 
                                   Toast.LENGTH_SHORT).show();
                    Toast.makeText(context, 
                                   "bearing:"+Float.toString(mCamPos.bearing) +
                                   "tilt:"+Float.toString(mCamPos.tilt) +
                                   "zoom"+Float.toString(mCamPos.zoom), 
                                   Toast.LENGTH_SHORT).show();
                } 
            }); 
    } 
 
    @Override 
    public void onResume() { 
        super.onResume(); 
        Toast.makeText(this, "Sensor Registered", Toast.LENGTH_SHORT).show();
        mSensorManager.registerListener(this, rotationSensor, 16000);
    } 
     
    @Override 
    protected void onPause() { 
        super.onPause(); 
        Toast.makeText(this, "Sensor Unregistered", Toast.LENGTH_SHORT).show();
        mSensorManager.unregisterListener(this);
    } 
     
    @Override 
    protected void onStop() { 
        Log.d("OnStop","Stop Periodic Updates");
        // If the client is connected 
        if (mLocationClient.isConnected()) {
            stopPeriodicUpdates(); 
        } 
        /* 
         * After disconnect() is called, the client is 
         * considered "dead". 
         */ 
        mLocationClient.disconnect();
        super.onStop(); 
    } 
     
    @Override 
    public void onConnectionFailed(ConnectionResult result) {