Skip to content

Commit aabf5c6

Browse files
committed
get last location
get last location
1 parent d35cfae commit aabf5c6

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hmkcode.locations"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="11"
9+
android:targetSdkVersion="19" />
10+
11+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
12+
<uses-permission android:name="android.permission.INTERNET" />
13+
14+
<application
15+
android:allowBackup="true"
16+
android:icon="@drawable/ic_launcher"
17+
android:label="@string/app_name"
18+
android:theme="@style/AppTheme" >
19+
<activity
20+
android:name=".MainActivity"
21+
android:label="@string/app_name" >
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
29+
<meta-data android:name="com.google.android.gms.version"
30+
android:value="@integer/google_play_services_version" />
31+
</application>
32+
33+
</manifest>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
tools:context="${relativePackage}.${activityClass}" >
6+
7+
<TextView
8+
android:id="@+id/tvLatlong"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="@string/hello_world" />
12+
13+
</RelativeLayout>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.hmkcode.locations;
2+
3+
import com.google.android.gms.common.ConnectionResult;
4+
import com.google.android.gms.common.api.GoogleApiClient;
5+
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
6+
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
7+
import com.google.android.gms.location.LocationServices;
8+
9+
import android.app.Activity;
10+
import android.location.Location;
11+
import android.os.Bundle;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
16+
public class MainActivity extends Activity implements
17+
ConnectionCallbacks, OnConnectionFailedListener {
18+
19+
GoogleApiClient mGoogleApiClient;
20+
Location mLastLocation;
21+
TextView tvLatlong;
22+
23+
@Override
24+
protected void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
setContentView(R.layout.activity_main);
27+
28+
tvLatlong = (TextView) findViewById(R.id.tvLatlong);
29+
30+
buildGoogleApiClient();
31+
32+
if(mGoogleApiClient!= null){
33+
mGoogleApiClient.connect();
34+
}
35+
else
36+
Toast.makeText(this, "Not connected...", Toast.LENGTH_SHORT).show();
37+
38+
39+
}
40+
41+
@Override
42+
public void onConnectionFailed(ConnectionResult arg0) {
43+
Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show();
44+
45+
}
46+
47+
@Override
48+
public void onConnected(Bundle arg0) {
49+
50+
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
51+
mGoogleApiClient);
52+
53+
if (mLastLocation != null) {
54+
tvLatlong.setText("Latitude: "+ String.valueOf(mLastLocation.getLatitude())+" - Longitude: "+
55+
String.valueOf(mLastLocation.getLongitude()));
56+
}
57+
58+
}
59+
60+
@Override
61+
public void onConnectionSuspended(int arg0) {
62+
Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show();
63+
64+
}
65+
66+
protected synchronized void buildGoogleApiClient() {
67+
mGoogleApiClient = new GoogleApiClient.Builder(this)
68+
.addConnectionCallbacks(this)
69+
.addOnConnectionFailedListener(this)
70+
.addApi(LocationServices.API)
71+
.build();
72+
}
73+
}

0 commit comments

Comments
 (0)