|
| 1 | +package com.hmkcode.android.location; |
| 2 | + |
| 3 | +import com.google.android.gms.common.ConnectionResult; |
| 4 | +import com.google.android.gms.common.GooglePlayServicesClient; |
| 5 | +import com.google.android.gms.location.LocationClient; |
| 6 | +import com.google.android.gms.location.LocationListener; |
| 7 | +import com.google.android.gms.location.LocationRequest; |
| 8 | + |
| 9 | +import android.app.Activity; |
| 10 | +import android.content.Intent; |
| 11 | + |
| 12 | +import android.location.Location; |
| 13 | +import android.os.Bundle; |
| 14 | +import android.provider.Settings; |
| 15 | +import android.widget.TextView; |
| 16 | +import android.widget.Toast; |
| 17 | + |
| 18 | +public class MainActivity extends Activity implements |
| 19 | + GooglePlayServicesClient.ConnectionCallbacks, |
| 20 | + GooglePlayServicesClient.OnConnectionFailedListener, |
| 21 | + LocationListener { |
| 22 | + |
| 23 | + // locations objects |
| 24 | + LocationClient mLocationClient; |
| 25 | + Location mCurrentLocation; |
| 26 | + LocationRequest mLocationRequest; |
| 27 | + |
| 28 | + TextView txtLong,txtLat; |
| 29 | + @Override |
| 30 | + protected void onCreate(Bundle savedInstanceState) { |
| 31 | + super.onCreate(savedInstanceState); |
| 32 | + // 1. setContnetView |
| 33 | + setContentView(R.layout.activity_main); |
| 34 | + |
| 35 | + // 2. get reference to TextView |
| 36 | + txtLong = (TextView) findViewById(R.id.txtLong); |
| 37 | + txtLat = (TextView) findViewById(R.id.txtLat); |
| 38 | + |
| 39 | + // 3. create LocationClient |
| 40 | + mLocationClient = new LocationClient(this, this, this); |
| 41 | + |
| 42 | + // 4. create & set LocationRequest for Location update |
| 43 | + mLocationRequest = LocationRequest.create(); |
| 44 | + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); |
| 45 | + // Set the update interval to 5 seconds |
| 46 | + mLocationRequest.setInterval(1000 * 5); |
| 47 | + // Set the fastest update interval to 1 second |
| 48 | + mLocationRequest.setFastestInterval(1000 * 1); |
| 49 | + |
| 50 | + |
| 51 | + } |
| 52 | + @Override |
| 53 | + protected void onStart() { |
| 54 | + super.onStart(); |
| 55 | + // 1. connect the client. |
| 56 | + mLocationClient.connect(); |
| 57 | + } |
| 58 | + @Override |
| 59 | + protected void onStop() { |
| 60 | + super.onStop(); |
| 61 | + // 1. disconnecting the client invalidates it. |
| 62 | + mLocationClient.disconnect(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + // GooglePlayServicesClient.OnConnectionFailedListener |
| 67 | + @Override |
| 68 | + public void onConnectionFailed(ConnectionResult connectionResult) { |
| 69 | + Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show(); |
| 70 | + } |
| 71 | + |
| 72 | + // GooglePlayServicesClient.ConnectionCallbacks |
| 73 | + @Override |
| 74 | + public void onConnected(Bundle arg0) { |
| 75 | + |
| 76 | + if(mLocationClient != null) |
| 77 | + mLocationClient.requestLocationUpdates(mLocationRequest, this); |
| 78 | + |
| 79 | + Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); |
| 80 | + |
| 81 | + if(mLocationClient != null){ |
| 82 | + // get location |
| 83 | + mCurrentLocation = mLocationClient.getLastLocation(); |
| 84 | + try{ |
| 85 | + |
| 86 | + // set TextView(s) |
| 87 | + txtLat.setText(mCurrentLocation.getLatitude()+""); |
| 88 | + txtLong.setText(mCurrentLocation.getLongitude()+""); |
| 89 | + |
| 90 | + }catch(NullPointerException npe){ |
| 91 | + |
| 92 | + Toast.makeText(this, "Failed to Connect", Toast.LENGTH_SHORT).show(); |
| 93 | + |
| 94 | + // switch on location service intent |
| 95 | + Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); |
| 96 | + startActivity(intent); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + @Override |
| 102 | + public void onDisconnected() { |
| 103 | + Toast.makeText(this, "Disconnected.", Toast.LENGTH_SHORT).show(); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + // LocationListener |
| 108 | + @Override |
| 109 | + public void onLocationChanged(Location location) { |
| 110 | + Toast.makeText(this, "Location changed.", Toast.LENGTH_SHORT).show(); |
| 111 | + mCurrentLocation = mLocationClient.getLastLocation(); |
| 112 | + txtLat.setText(mCurrentLocation.getLatitude()+""); |
| 113 | + |
| 114 | + txtLong.setText(mCurrentLocation.getLongitude()+""); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments