android location位置位置決め

8259 ワード

実装機能:現在位置を特定し、詳細を表示し、Googleマップを拡張

package com.ld.gps;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;

public class AppLocationGpsActivity extends Activity {

private static final int TWO_MINUTES = 1000 * 60 * 2;
private LocationManager locationManager;

private String providerName;

private Location currentBestLocation;

private Location location;

private Address address;

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

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Location Provider

providerName=getProviderName();

// , GPS

openGPS();

//

currentBestLocation= locationManager.getLastKnownLocation(providerName);

location=new Location(LocationManager.GPS_PROVIDER);

if(isBetterLocation(location,currentBestLocation)){
//

updateWithNewLocation(location);
providerName=location.getProvider();

}else{
//

updateWithNewLocation(currentBestLocation);

}


// locationListener , 2 、 3 gps 。 2 ,

// listener , 3 , listener

locationManager.requestLocationUpdates(providerName, 2000, 10,locationListener);
}

// GPS , , GPS

private void openGPS() {

if (locationManager
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)

|| locationManager
.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)

) {

Toast.makeText(this, " ! ", Toast.LENGTH_SHORT).show();

return;

}

Toast.makeText(this, " ! ", Toast.LENGTH_SHORT).show();

// GPS

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

startActivityForResult(intent, 0);

}

// Location Provider

private String getProviderName() {

//

Criteria criteria = new Criteria();

// :

criteria.setAccuracy(Criteria.ACCURACY_FINE);

// :

criteria.setAltitudeRequired(false);

// :

criteria.setBearingRequired(false);

// :

criteria.setCostAllowed(true);

// :

criteria.setPowerRequirement(Criteria.POWER_LOW);

// provider , 2 true , provider ,
// provider

return locationManager.getBestProvider(criteria, true);

}

// Gps

private final LocationListener locationListener = new LocationListener() {

//

public void onLocationChanged(Location location) {

updateWithNewLocation(location);

}

// provider

public void onProviderDisabled(String provider) {

updateWithNewLocation(null);

}

// provider

public void onProviderEnabled(String provider) {
}

// provider

public void onStatusChanged(String provider, int status,

Bundle extras) {
}

};

// Gps ,

private void updateWithNewLocation(Location location) {

String latLongString;

TextView myLocationText = (TextView) findViewById(R.id.text);

if (location != null) {

double lat = location.getLatitude();

double lng = location.getLongitude();

latLongString = " :" + lat + "
:" + lng;

} else {

latLongString = " ";

}
List
addrList=getAddressbyGeoPoint(location);
String addr=" :";
if(addrList!=null && !addrList.isEmpty())
addr+=parseAddr(addrList.get(0));
myLocationText.setText(" :
" +

latLongString + "
" + addr);

}

private String parseAddr(Address address){

return address.getAddressLine(0)+address.getAddressLine(1)+address.getAddressLine(2)+address.getFeatureName();
}
//

private List
getAddressbyGeoPoint(Location location) {

List
result = null;

// Location GeoPoint

// GeoPoint gp =getGeoByLocation(location);

try {

if (location != null) {

// Geocoder , Geocoder

Geocoder gc = new Geocoder(this, Locale.getDefault());

result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**
* Determines whether one Location reading is better than the current
* Location fix
*
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new
* one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}

// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;

// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be
// worse
} else if (isSignificantlyOlder) {
return false;
}

// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;

// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());

// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}