AndroidManifest.xml 增加 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

public class ditu extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName=Context.LOCATION_SERVICE;
locationManager=(LocationManager)this.getSystemService(serviceName);
//查询条件
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider=locationManager.getBestProvider(criteria,true);
Location location=locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
//设置监听器,自动更新的最小时间为间隔1秒,最小位移变化超过5米
locationManager.requestLocationUpdates(provider, 1000, 5, locationListener);

}
private final LocationListener locationListener=new LocationListener(){

public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}

public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}

};
private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText;
myLocationText=(TextView)this.findViewById(R.id.myLocationText);
if(location!=null){
double lat=location.getLatitude();
double lng=location.getLongitude();
latLongString="维度:"+lat+"\n经度"+lng;
}else{
latLongString="无法获取地理信息";
}
myLocationText.setText("您所在位置为:"+latLongString);
}
}


哈哈,只要手机开了GPS,你就能随时定位自己的位置,可惜显示出来的是经纬度,如果结合google API,那就能显示在地图上了。

更多相关文章

  1. 2011.09.07——— android zxing 条形码
  2. 2013.06.26——— android 关于Scroller
  3. 2011.09.07——— android zxing 条形码
  4. android中GPS信息的获取
  5. Android之地理信息服务·Android的定位服务
  6. Android百度地图之位置定位和附近查找代码简单实现 (上)
  7. 一个java android 时钟 非常精致!android 小创意
  8. google map my demo
  9. google map my demo

随机推荐

  1. 基于CentOS系统安装OceanBase数据库
  2. 基于CentOS系统安装OceanBase数据库
  3. javascript-基础(六)
  4. HTML-Canvas的优越性能以及实际应用
  5. 函数参数及返回值、模板字面量及模板函数
  6. Android(安卓)- 永不锁屏,开机不锁屏,删除
  7. Android实现手势滑动(左滑和右滑)
  8. Android(安卓)LayoutInflater 源码分析
  9. 【demo记录】极光推送(android app访问服
  10. ubuntu下eclipse配置android SDK