
The above image shows user's current latitude and longitude.
Steps to Get the above result.
Permissions that is to be added in Manifest file for accessing current location.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Why ACCESS_FINE_LOCATION?
Access fine location gives permission to both GPS_PROVIDER and NETWORK_PROVIDER, it provides more accurate locations.
Why ACCESS_COARSE_LOCATION?
For determining location it gives permission to only NETWORK_PROVIDER, it provides less accurate locations.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eazy.getmylocation.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tvaddress"
android:layout_centerInParent="true" />
</RelativeLayout>
MainActivity.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
TextView tvaddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},1);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
tvaddress= (TextView) findViewById(R.id.tvaddress);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
tvaddress.setText(location.getLatitude()+"-"+location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}, null);
}
}
In the above code we have use ActivityCompact class to give permission via java code which is necessary for marshmallow devices.
requestSingleUpdate(String provider,LocationListener listener, Looper looper) is method of LocationManager which takes three parameters. where provider may be GPS_PROVIDER or NETWORK_PROVIDER
What is GPS_PROVIDER
Determines the location of the users using satellites.
What is NETWORK_PROVIDER
Determines location using cell towers,wifi access points etc.
Thank you Ma’am for this program and it's very easy to understand And how to get user's current location in google map ?
ReplyDeleteSure i'll upload the example for the same soon
ReplyDelete