Demo:
1)MainActivity.java:
package com.venky.getcurrentlocation;
import
android.app.Activity;
import
android.content.Context;
import
android.location.Location;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
TextView tv = (TextView)
findViewById(R.id.tv);
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lati =
location.getLatitude();
double longi =
location.getLongitude();
tv.setText("Your
Location\nLatitude=>" + lati + "\nLongitude=>" + longi);
}
}
2)activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#ffffff"
/>
</RelativeLayout>
3)Required Permission:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
4)Downlod this code Click Here
No comments:
Post a Comment