location problem
Hi to all im new to android and i have a small problem and i would really appreciate if someone can help me
first im trying to show all available location providers and its not working and 2nd when ever i run the it i don't get any location information from the best available provider (i have my wifi and network providers on) thanks in advance
package com.paad.whereami;
import java.io.IOException;
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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
boolean enabledOnly = true;
List<String> providers = locationManager.getProviders(enabledOnly);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setSpeedRequired(true);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 1,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); 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 (IOException e) {}
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString + "\n" + addressString);
}
}
I had also same problem before, after searching a lot...came to solution and makes it possibale to get instant location of device through following code...actuallu we can not have gps responce instantly so we can have our location on the basis of cell-tower or wifi. so enable one of them to get instant location of your device..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leopard_screen);
FindLocation(this);
}
public void FindLocation(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (network_enabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
Log.i("@@@@@@@@@@ Network provider is enabled", "Network Provider");
} else {
Toast.makeText(LeopardScreen.this,
"Network provider is not enabled", 2000);
}
if (gps_enabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListenerGPS);
Log.i("@@@@@@@@@@ GPS provider is enabled", "GPS Provider");
} else {
Toast.makeText(LeopardScreen.this, "GPS provider is not enabled",
2000);
}
if(!network_enabled && !gps_enabled) {
currentLocation = getMyLastKnownLocation();
currentLatitude = currentLocation.getLatitude();
currentLongitude = currentLocation.getLongitude();
Log.i("@@@@@@@@ Both location provider disabled",
"getMylastKnownLocation = "+String.valueOf(currentLatitude)
+ " : " + String.valueOf(currentLongitude));
Toast.makeText(LeopardScreen.this,"LastKnownLocation\n"+String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 3000).show();
Intent intent = new Intent(LeopardScreen.this, mainActivity.class);
startActivity(intent);
}
}
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
handler.removeCallbacks(runnable);
Log.i("@@@@@@@@ Inside FindLocation", "Inside FindLocation");
Toast.makeText(
LeopardScreen.this,"Network Location \n"+
String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 5000).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
LocationListener locationListenerGPS = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
Log.i("@@@@@@@@@@ Inside onLocationChangedGPS", String
.valueOf(currentLatitude)
+ " : " + String.valueOf(currentLongitude));
Toast.makeText(
LeopardScreen.this,
"GPS Location \n" + String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 5000).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
public Location getMyLastKnownLocation () {
Location locNetwrok = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location locGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(locNetwrok != null)
return locNetwrok;
else if(locGPS != null)
return locGPS;
return null;
}
void updateLocation(Location location) {
currentLocation = location;
currentLatitude = currentLocation.getLatitude();
currentLongitude = currentLocation.getLongitude();
Log.i("@@@@@@@@ Inside LeopardScreen locationChanged",
"locationChanged");
}
Don't Forgot to add in Menifeast
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
精彩评论