android定位个人当前位置

本文介绍了一种使用Android系统获取设备地理位置信息的方法,包括经纬度及详细地址,并提供了完整的XML布局文件、清单文件配置以及Java代码实现。
废话不多说,直接进入代码块:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
   
   <TextView  
    android:id="@+id/longitude"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="longitude:" 
    /> 
<TextView 
   android:id="@+id/latitude"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
   android:text="latitude:" 
    /> 
<TextView 
    android:id="@+id/address"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
   />
   
</LinearLayout>
   清单文件( <uses-library android:name="com.google.android.maps" />
) 中最重要的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" http://schemas.android.com/apk/res/android"
    package="cn.com"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TestAddresActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.INTERNET" />
</manifest>
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;
/** Called when the activity is first created. */
@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<Address> lstAddress = gc.getFromLocation(geoLatitude,
      geoLongitude, 1);
    if (lstAddress.size() > 0) {
     result = lstAddress.get(0);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
}
}
注意: 若在模拟器上运行,必须在DDMS上send 经纬度.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值