Android获取手机当前所在经纬度请参考:http://blog.youkuaiyun.com/a511341250/article/details/40509931
manifest中要加的permission和activity_main.xml 参照上。
MainActivity.java
package com.example.test;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.widget.EditText;
public class MainActivity extends Activity {
LocationManager lm;
EditText et;
LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location){
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Location l = lm.getLastKnownLocation(provider);
updateView(l);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.et);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String bestProvider = lm.getBestProvider(getCriteria(), true);
Location l = lm.getLastKnownLocation(bestProvider);
updateView(l);
lm.requestLocationUpdates(bestProvider, 5000, 8, ll);
}
private Criteria getCriteria() {
// TODO Auto-generated method stub
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setSpeedRequired(false);
c.setCostAllowed(false);
c.setBearingRequired(false);
c.setAltitudeRequired(false);
c.setPowerRequirement(Criteria.POWER_LOW);
return c;
}
public void updateView(Location newLocation)
{
if(newLocation !=null){
String latitude = String.valueOf(newLocation.getLatitude());
String longitude = String.valueOf(newLocation.getLongitude());
double Latitude = newLocation.getLatitude();
double Longitude = newLocation.getLongitude();
String city = getAddressbyGeoPoint(Latitude,Longitude);
et.setText("您现在的位置是\n维度:");
et.append(latitude);
et.append("\n经度:");
et.append(longitude);
et.append("\n城市:");
et.append(city);
}
else{
et.getEditableText().clear();
}
}
//从地址Geopoint取得Address
public String getAddressbyGeoPoint(double Latitude, double Longitude)
{
String strReturn = "";
try
{
/* 创建GeoPoint不等于null */
// if (gp != null)
// {
/* 创建Geocoder对象,用于获得指定地点的地址 */
Geocoder gc = new Geocoder(MainActivity.this, Locale.getDefault());
/* 取出地理坐标经纬度*/
double geoLatitude = Latitude;
double geoLongitude = Longitude;
/* 自经纬度取得地址(可能有多行)*/
List<Address> lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1);
StringBuilder sb = new StringBuilder();
/* 判断地址是否为多行 */
if (lstAddress.size() > 0)
{
Address adsLocation = lstAddress.get(0);
for (int i = 0; i < adsLocation.getMaxAddressLineIndex(); i++)
{
sb.append(adsLocation.getAddressLine(i)).append("\n"); //精确的地址和附近的代表建筑物
}
sb.append(adsLocation.getLocality()).append("\n"); //当前经纬度所在的城市(市)
// sb.append(adsLocation.getPostalCode()).append("\n");
// sb.append(adsLocation.getCountryName());
}
/* 将取得到的地址组合后放到stringbuilder对象中输出用 */
strReturn = sb.toString();
// }
}
catch(Exception e)
{
e.printStackTrace();
}
return strReturn;
}
}
参考kaka2008的回复:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=23068&page=1#pid216151
用到的jar包Android_Location_V1.1.1.jar