廢話不多說,直接進入代碼塊:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="longitude:"
/>
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="latitude:"
/>
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
清單檔案(
) 中最重要的代碼
package="cn.com"
android:versionCode="1"
android:versionName="1.0" >
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".TestAddresActivity"
android:label="@string/app_name" >
java代碼:
package cn.com;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class TestAddresActivity extends Activity {
private TextView longitude;
private TextView latitude;
private TextView address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
longitude = (TextView) findViewById(R.id.longitude);
latitude = (TextView) findViewById(R.id.latitude);
address = (TextView) findViewById(R.id.address);
Location mLocation = getLocation(this);
GeoPoint gp = getGeoByLocation(mLocation);
Address mAddress = getAddressbyGeoPoint(this, gp);
longitude.setText("Longitude: " + mLocation.getLongitude());
latitude.setText("Latitude: " + mLocation.getLatitude());
address.setText("Address: " + mAddress.getCountryName() + ","
+ mAddress.getLocality() + "," + mAddress.getThoroughfare());
}
// Get the Location by GPS or WIFI
public Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
// 通過Location擷取GeoPoint
public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
if (location != null) {
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}
// 通過GeoPoint來擷取Address
public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
Address result = null;
try {
if (gp == null) {
Toast.makeText(getApplicationContext(), "抱歉,暫未擷取到資訊", 1).show();
}
if (gp != null) {
Geocoder gc = new Geocoder(cntext, Locale.CHINA);
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
List
lstAddress = gc.getFromLocation(geoLatitude,
geoLongitude, 1);
if (lstAddress.size() > 0) {
result = lstAddress.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
注意: 若在模拟器上運作,必須在DDMS上send 經緯度.