Showing posts with label Android code sample: LocationSource and LocationListener. Show all posts
Showing posts with label Android code sample: LocationSource and LocationListener. Show all posts

Wednesday, February 27, 2013

Implement OnMyLocationChangeListener for Google Maps Android API v2

With Google Play services v3.0, and Android SDK Platform-tools with Google Play Services updated, we can implement OnMyLocationChangeListener, called when the Location of the My Location dot has changed.

Implement OnMyLocationChangeListener for Google Maps Android API v2


Modify the Java code from the last exercise "Draw Circle on GoogleMap".
package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
 implements OnMyLocationChangeListener{
 
 final int RQS_GooglePlayServices = 1;
 private GoogleMap myMap;
 
 TextView tvLocInfo;
 
 Circle myCircle;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  tvLocInfo = (TextView)findViewById(R.id.locinfo);
  
  FragmentManager myFragmentManager = getFragmentManager();
  MapFragment myMapFragment 
   = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
  myMap = myMapFragment.getMap();

  myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

  myMap.setMyLocationEnabled(true);
  myMap.setOnMyLocationChangeListener(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.activity_main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
     case R.id.menu_legalnotices:
      String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
        getApplicationContext());
      AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
      LicenseDialog.setTitle("Legal Notices");
      LicenseDialog.setMessage(LicenseInfo);
      LicenseDialog.show();
         return true;
     }
  return super.onOptionsItemSelected(item);
 }

 @Override
 protected void onResume() {

  super.onResume();

  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
  
  if (resultCode == ConnectionResult.SUCCESS){
   Toast.makeText(getApplicationContext(), 
     "isGooglePlayServicesAvailable SUCCESS", 
     Toast.LENGTH_LONG).show();
  }else{
   GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
  }
  
 }

 @Override
 public void onMyLocationChange(Location location) {
  tvLocInfo.setText("New circle added@" + location.toString());
  
  LatLng locLatLng = new LatLng(location.getLatitude(), location.getLongitude());
  double accuracy = location.getAccuracy();
  
  if(myCircle == null){
   CircleOptions circleOptions = new CircleOptions()
   .center(locLatLng)   //set center
   .radius(accuracy)   //set radius in meters
   .fillColor(Color.RED)
   .strokeColor(Color.BLACK)
   .strokeWidth(5);
   
   myCircle = myMap.addCircle(circleOptions);
  }else{
   myCircle.setCenter(locLatLng);
   myCircle.setRadius(accuracy);
  }
  
  myMap.animateCamera(CameraUpdateFactory.newLatLng(locLatLng));

 }

}


download filesDownload the files.


The series:
A simple example using Google Maps Android API v2, step by step.

Thursday, January 24, 2013

Implement LocationSource and LocationListener for Google Maps Android API v2

Example to Implement LocationSource and LocationListener for Google Maps Android API v2.

Implement LocationSource and LocationListener for Google Maps Android API v2


package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.MapFragment;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
  implements LocationSource, LocationListener{
 
 final int RQS_GooglePlayServices = 1;
 private GoogleMap myMap;
 TextView tvLocInfo;
 
 LocationManager myLocationManager = null;
 OnLocationChangedListener myLocationListener = null;
 Criteria myCriteria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLocInfo = (TextView)findViewById(R.id.locinfo);
        
        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment 
         = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();
        
        myMap.setMyLocationEnabled(true);
        
        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        
        myCriteria = new Criteria();
        myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.menu_legalnotices:
   String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
     getApplicationContext());
   AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
   LicenseDialog.setTitle("Legal Notices");
   LicenseDialog.setMessage(LicenseInfo);
   LicenseDialog.show();
   return true; 
  }
  return super.onOptionsItemSelected(item); 
 }

 @Override
 protected void onResume() {
  super.onResume();
  
  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
  
  if (resultCode == ConnectionResult.SUCCESS){
   Toast.makeText(getApplicationContext(), 
     "isGooglePlayServicesAvailable SUCCESS", 
     Toast.LENGTH_LONG).show();
   
   //Register for location updates using a Criteria, and a callback on the specified looper thread.
   myLocationManager.requestLocationUpdates(
     0L,    //minTime
     0.0f,    //minDistance
     myCriteria,  //criteria
     this,    //listener
     null);   //looper
   
   //Replaces the location source of the my-location layer.
      myMap.setLocationSource(this);
   
  }else{
   GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices); 
  }
  
 }

 @Override
 protected void onPause() {
  myMap.setLocationSource(null);
  myLocationManager.removeUpdates(this);
     
  super.onPause();
 }

 @Override
 public void activate(OnLocationChangedListener listener) {
  myLocationListener = listener;
 }

 @Override
 public void deactivate() {
  myLocationListener = null;
 }

 @Override
 public void onLocationChanged(Location location) {
  if (myLocationListener != null) {
   myLocationListener.onLocationChanged(location);
   
   double lat = location.getLatitude();
   double lon = location.getLongitude();
   
   tvLocInfo.setText(
     "lat: " + lat + "\n" +
     "lon: " + lon);
  }
 }

 @Override
 public void onProviderDisabled(String arg0) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onProviderEnabled(String arg0) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  // TODO Auto-generated method stub
  
 }

}


download filesDownload the files.

Next:
- Detect and animate to user location


The series:
A simple example using Google Maps Android API v2, step by step.