Android座標を取得し、完全なアドレスを出力
3239 ワード
GPS ,
, LBS
Java :
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
< /uses-permission>
package com.location;
import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LocationFinder extends Activity {
/** Called when the activity is first created. */
static String address = " ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// double latitude=0,longitude=0;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
while(location==null){
locationManager.requestLocationUpdates("gps", 60000, 1, locationListener);
}
double latitude = location.getLatitude(); //
double longitude = location.getLongitude(); //
StringBuilder sb = new StringBuilder();
sb.append(" :").append(String.valueOf(latitude)).append("
")
.append(" :").append(String.valueOf(longitude)).append("
");
//
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> add = gc.getFromLocation(latitude, longitude, 1);
StringBuilder bb = new StringBuilder();
if (add.size() > 0) {
Address ad = add.get(0);
bb.append(ad.getAddressLine(0)).append("
");
bb.append(ad.getAddressLine(1)).append("
");
bb.append(ad.getAddressLine(2)).append("
");
sb.append(bb);
}
} catch (Exception e) {
}
address = sb.toString();
TextView text = (TextView) findViewById(R.id.tv);
text.setText(address);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) { // , Provider ,
// log it when the location changes
if (location != null) {
Log.i("SuperMap",
"Location changed : Lat: " + location.getLatitude()
+ " Lng: " + location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
// Provider disable , GPS
}
public void onProviderEnabled(String provider) {
// Provider enable , GPS
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider 、
}
};
}