Monday, October 12, 2009

Location Manager Examples

Here theres is a small program regarding location manager and location class

package com.vinnysoft.ami;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class WhereAmI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);

Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);

// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
}

private final LocationListener locationListener = new LocationListener()
{

@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
myLocation = (TextView) findViewById(R.id.myLocation);

String addressString = "no address found";

if(location!=null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;

double lattitude = location.getLatitude();
double longitude = location.getLongitude();

Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List
addresses= gc.getFromLocation(lattitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size()>0) {
Address address = addresses.get(0);
for(int i =0;i sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
}catch (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
myLocation.setText("your Current Position is :\n" +latLong + "\n " + addressString );
}
}


add mapview in main.xml file

give the permission sin android manifest file