Sunday, March 11, 2012

How to get your location using Android; GPS locator using android


This is an application which locates where you are. It uses your mobile GPS provider, or you GPRs connectivity or even the WiFi connectivity you can access.


First  Start up your eclipse, Go to File->New-> Project -> Android Project. Then click "Next".

Give project name, "edayansLocator". Click "Next". Select android version you have (I have selected Android 4.3).  and click "Next".

On the next window give  package name as "com.edayans.locator". As shown below and click "Finish".



Now select  "res -> values-> strings.xml". open it and click "Add" button and select "String". Then click "OK".   See the images below.




Give  Name as "getLocation" and Value as "Get My Location"; As shown below.


Now select "res -> Layout-> main.xml". Switch to graphical layout(indicated by  yellow color arrow in the image below).

From the "Form Widgets" in the panel on the left side, select "Button " (indicated by red arrow) and drag it to screen. (See image below).




Now right click on the "Button" and click on "Edit Text..." and select "getLocation" from the new window and click "OK". see images below.





Now again Right click on the Button and go to "Other properties -> All By Name -> On Click " ; in the new window type "onClickDoThis".





Now go to "src -> com.edayans.locator ->EdayansLocatorActivity.java " the  copy and paste  the code given below on their.


package com.edayans.locator;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class EdayansLocatorActivity extends Activity {
    /** Called when the activity is first created. */
   
    private String provider;
    private LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onClickDoThis(View v) {
       
        showCurrentLocation();
    }
    protected void showCurrentLocation() {
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            double lat = location.getLatitude();
            double lon = location.getLongitude();
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    lon, lat);
            Toast.makeText(EdayansLocatorActivity.this, message,
                    Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(EdayansLocatorActivity.this, "no location available",
                    Toast.LENGTH_LONG).show();
        }
    }

}


Now Select "AndroidManifest.xml" from the panel and got to "Permissions" (indicated by red and brown arrows) and click "Add" button (on the upper right side in the image).  Then  select "Uses Permission" and click "OK".





Then select "android.permission.ACCESS_COARSE_LOCATION". See the image below...


 


Then add  "android.permission.ACCESS_FINE_LOCATION", and
 also "android.permission.ACCESS_INTERNET".




Now take a look what we have added;

ACCESS_COARSE_LOCATION : This allows you to access the location through a network like  WiFi, Cell-ID etc..

ACCESS_FINE_LOCATION : This allows you to access the location through GPS.

Now save the project and install the .apk file to any android mobile. click on the button and get your location.


Note:  This application is not for running on any simulator. To run this application on simulator we have to provide a "mock" location and also a permission to use "MOCK_LOCATION". But any way you can install this application on any android device and feel it.


wish you all the best.....


No comments:

Post a Comment