To find a current location

by Thursday, March 15, 2012 0 comments
use this code for finding current location.


curlocation.java:
package com.collabera.labs.sai.maps;








public class curlocation extends MapActivity implements LocationListener {
    /** Called when the activity is first created. */
   
    TextView myLoc = null;
   
    MapView    myMapView = null;
   
    MapController myMC = null;
   
    GeoPoint geoPoint = null;
   
    double latitude=13.058587, longitude=80.216717;
   
    Calendar c = Calendar.getInstance(); 
   
    SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a"); 
    String dt = df3.format(c.getTime());
  
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // Creating TextBox displaying Latitude, Longitude
        myLoc = (TextView) findViewById(R.id.id1);
        String currentLocation = "I'm in: Lat: " + latitude + " Lng: " + longitude + "time:" + dt;
        myLoc.setText(currentLocation);
       
       
        // Creating and initializing Map
        myMapView = (MapView) findViewById(R.id.myGMap);
        geoPoint = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
        myMapView.setSatellite(false);
       
        //Getting the MapController to fine tune settings
        myMC = myMapView.getController();
        myMC.setCenter(geoPoint);
        myMC.setZoom(16);
       
        // Add a location mark
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
        List list = myMapView.getOverlays();
        list.add(myLocationOverlay);
       
        // Adding zoom controls to Map
        myMapView.setBuiltInZoomControls(true);
        myMapView.displayZoomControls(true);
               
        // Getting locationManager and reflecting changes over map if distance travel by
        // user is greater than 500m from current location.
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
    }

   
   
From this coding u get current location of Latitude and Longitude. By getting latitude&longitude we can convert it into address using Reverse Geo-coding method.  

karthik nagarajan

Androider

Welcome to Android-Action Blog. I’m a normal guy, who is passionate about Mobile Coding. Here I am writing about Android. Happy learning

0 comments:

Post a Comment