Sunday, 27 January 2013

Finding Current Location in Android

MainActivity.java:

package com.example.findingcurrentlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LocationManager locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location location = locationmanager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

double longi = location.getLatitude();
double latti = location.getLongitude();

String landl = "lattitude" + latti + "longitutde" + longi;

Toast.makeText(getApplicationContext(), landl, 3000).show();
}

}

AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.findingcurrentlocation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>