我在android上開發一個檢索用戶位置的應用程序。其實我需要使用最可靠的服務,因爲我需要一個非常精確的位置。如何擁有一個非常精確的地理位置?
我不知道現在該做什麼,特別是因爲我是初學者。
謝謝你幫我
我在android上開發一個檢索用戶位置的應用程序。其實我需要使用最可靠的服務,因爲我需要一個非常精確的位置。如何擁有一個非常精確的地理位置?
我不知道現在該做什麼,特別是因爲我是初學者。
謝謝你幫我
您將需要用戶LocationManager
獲取用戶的經度和緯度。 和GeoCoder
獲取地址名稱和其他信息(稱爲反向地理編碼) 請參考以下文檔。
http://developer.android.com/reference/android/location/Geocoder.html
http://developer.android.com/reference/android/location/LocationManager.html
下面的代碼將有助於you.But我建議經過上述鏈接,以獲得正確的認識。
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 GeoSample extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmenu);
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<Address> addresses = gc.getFromLocation(lattitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0)
{
Address address = (Address) addresses.get(0);
sb.append(address.getAddressLine(0)).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);
}
}
記住GPS提供商將爲您提供準確information.But將需要一些時間 和消耗更多的電池。
希望這會有所幫助。
你確定這是手段,更精確? – 2012-03-16 05:09:11
那就是我說的使用String provider = LocationManager.GPS_PROVIDER; 以獲得精確的位置。但是,獲取位置並節省更多能源需要時間。 – 2012-03-16 05:11:01
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !network_enabled) {
// show alert
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
使用GPS ........ – 2012-03-16 04:38:12