Skip to content

Commit ba184ea

Browse files
committed
get location
get location
1 parent 17af32d commit ba184ea

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hmkcode.android.location"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
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+
15+
<application
16+
android:allowBackup="true"
17+
android:icon="@drawable/ic_launcher"
18+
android:label="@string/app_name"
19+
android:theme="@style/AppTheme" >
20+
<activity
21+
android:name=".MainActivity"
22+
android:label="@string/app_name" >
23+
<intent-filter>
24+
<action android:name="android.intent.action.MAIN" />
25+
26+
<category android:name="android.intent.category.LAUNCHER" />
27+
</intent-filter>
28+
</activity>
29+
30+
<meta-data android:name="com.google.android.gms.version"
31+
android:value="@integer/google_play_services_version" />
32+
33+
</application>
34+
35+
36+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
6+
7+
8+
<TextView
9+
android:id="@+id/txtLong"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:layout_below="@+id/textView1"
13+
android:text="Long"
14+
android:textSize="24dp" />
15+
16+
17+
<TextView
18+
android:id="@+id/txtLat"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:layout_below="@+id/txtLong"
22+
android:text="Lat"
23+
android:textSize="24dp" />
24+
25+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)