Hello friends,

In this blog post I am going to write about how to get the location coordinates in an android app and how to get the city name using those coordinates.

I have tried this app already and its working good.Please enable your gps settings and mobile data before using this app.

Here is the code

MainActivity.Java

1:  package devicelocation.abhi.com.newgetuserlocation;  
2:    
3:  import android.content.Context;  
4:  import android.location.Address;  
5:  import android.location.Criteria;  
6:  import android.location.Geocoder;  
7:  import android.location.Location;  
8:  import android.location.LocationManager;  
9:  import android.support.v7.app.ActionBarActivity;  
10:  import android.os.Bundle;  
11:  import android.util.Log;  
12:  import android.view.Menu;  
13:  import android.view.MenuItem;  
14:  import android.view.View;  
15:  import android.widget.Button;  
16:  import android.widget.TextView;  
17:  import android.widget.Toast;  
18:    
19:  import java.io.IOException;  
20:  import java.util.List;  
21:  import java.util.Locale;  
22:    
23:    
24:  public class MainActivity extends ActionBarActivity {  
25:    
26:    
27:    String cityname;  
28:    
29:    TextView latTextView;  
30:    TextView longTextView;  
31:    TextView locTextView;  
32:    @Override  
33:    protected void onCreate(Bundle savedInstanceState) {  
34:      super.onCreate(savedInstanceState);  
35:      setContentView(R.layout.activity_main);  
36:      latTextView=(TextView)findViewById(R.id.LattextView);  
37:      longTextView=(TextView)findViewById(R.id.LongtextView);  
38:      locTextView=(TextView)findViewById(R.id.LocationtextView);  
39:    
40:    
41:      //TO get the location,manifest file is added with 2 permissions  
42:      //Location Manager is used to figure out which location provider needs to be used.  
43:      LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);  
44:    
45:    
46:      //Best location provider is decided by the criteria  
47:      Criteria criteria=new Criteria();  
48:      //location manager will take the best location from the criteria  
49:      locationManager.getBestProvider(criteria, true);  
50:    
51:      //nce you know the name of the LocationProvider, you can call getLastKnownPosition() to find out where you were recently.  
52:      Location location=locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria,true));  
53:    
54:      Geocoder gcd=new Geocoder(getBaseContext(), Locale.getDefault());  
55:      latTextView.setText(String.valueOf(location.getLatitude()));  
56:      longTextView.setText(String.valueOf(location.getLongitude()));  
57:      Log.d("Tag","1");  
58:      List<Address> addresses;  
59:    
60:        try {  
61:          addresses=gcd.getFromLocation(location.getLatitude(),location.getLongitude(),1);  
62:          if(addresses.size()>0)  
63:    
64:          {  
65:            //while(locTextView.getText().toString()=="Location") {  
66:              cityname = addresses.get(0).getLocality().toString();  
67:              locTextView.setText(cityname);  
68:            // }  
69:          }  
70:    
71:        } catch (IOException e) {  
72:          e.printStackTrace();  
73:    
74:        }  
75:    
76:    
77:      }  
78:    
79:    
80:    
81:    
82:    @Override  
83:    public boolean onCreateOptionsMenu(Menu menu) {  
84:      // Inflate the menu; this adds items to the action bar if it is present.  
85:      getMenuInflater().inflate(R.menu.menu_main, menu);  
86:      return true;  
87:    }  
88:    
89:    @Override  
90:    public boolean onOptionsItemSelected(MenuItem item) {  
91:      // Handle action bar item clicks here. The action bar will  
92:      // automatically handle clicks on the Home/Up button, so long  
93:      // as you specify a parent activity in AndroidManifest.xml.  
94:      int id = item.getItemId();  
95:    
96:      //noinspection SimplifiableIfStatement  
97:      if (id == R.id.action_settings) {  
98:        return true;  
99:      }  
100:    
101:      return super.onOptionsItemSelected(item);  
102:    }  
103:  }  
104:    

activity_main.xml

1:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2:    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
3:    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
4:    android:paddingRight="@dimen/activity_horizontal_margin"  
5:    android:paddingTop="@dimen/activity_vertical_margin"  
6:    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"  
7:    android:orientation="vertical"  
8:    android:weightSum="1">  
9:    
10:    <TextView  
11:      android:layout_width="match_parent"  
12:      android:layout_height="wrap_content"  
13:      android:textAppearance="?android:attr/textAppearanceLarge"  
14:      android:text="Large Text"  
15:      android:id="@+id/LattextView"  
16:    
17:      android:padding="20dp"/>  
18:    
19:    <TextView  
20:      android:layout_width="wrap_content"  
21:      android:layout_height="wrap_content"  
22:      android:textAppearance="?android:attr/textAppearanceLarge"  
23:      android:text="Large Text"  
24:      android:id="@+id/LongtextView"  
25:      android:padding="20dp"/>  
26:    
27:    <TextView  
28:      android:padding="20dp"  
29:      android:layout_width="match_parent"  
30:      android:layout_height="wrap_content"  
31:      android:textAppearance="?android:attr/textAppearanceLarge"  
32:      android:text="Location"  
33:      android:id="@+id/LocationtextView"  
34:      android:layout_centerVertical="true"  
35:      android:layout_alignParentLeft="true"  
36:      android:layout_alignParentStart="true" />  
37:    
38:    
39:    
40:  </LinearLayout>  
41:    

AndroidManifest.xml 

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
3:    package="devicelocation.abhi.com.newgetuserlocation" >  
4:    
5:    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
6:    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
7:    <application  
8:      android:allowBackup="true"  
9:      android:icon="@mipmap/ic_launcher"  
10:      android:label="@string/app_name"  
11:      android:theme="@style/AppTheme" >  
12:      <activity  
13:        android:name=".MainActivity"  
14:        android:label="@string/app_name" >  
15:        <intent-filter>  
16:          <action android:name="android.intent.action.MAIN" />  
17:    
18:          <category android:name="android.intent.category.LAUNCHER" />  
19:        </intent-filter>  
20:      </activity>  
21:    </application>  
22:    
23:  </manifest>  
24:    

You can also download the full source code here.

https://drive.google.com/file/d/0B2Y9-jpvsfOBYzRrdTNFSEpZbjA/view?usp=sharing

Cheers!

Please comment if you have any thoughts or any query related to the post.

Advertisements