小白教你如何在android手机上获取gps和基站的经纬度

android手机获取gps和基站的经纬度地址

         在导入这个demo时,要确保你的手机打开了GPS,要不就会报错,当然你也可以优化一下这个程序,下面先给大家这两个好用的代码先,不过我没加入进去,大家可以试着加一下,以优化我的代码。
/**
	 * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
	 * 
	 * @param context
	 * @return true 表示开启
	 */
	public static final boolean isGPSOPen(final Context context) {
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		// 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
		boolean gps = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂                //密的深林等)密集的地方定位)
		boolean network = locationManager
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (gps || network) {
			return true;
		}
		return false;
	}

	/**
	 * 强制帮用户打开GPS
	 * 
	 * @param context
	 */
	public static final void openGPS(Context context) {
		Intent GPSIntent = new Intent();
		GPSIntent.setClassName("com.android.settings",
				"com.android.settings.widget.SettingsAppWidgetProvider");
		GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
		GPSIntent.setData(Uri.parse("custom:3"));
		try {
			// Retrieve(检索) a PendingIntent that will perform a broadcast, like
			// calling Context.sendBroadcast().
			PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

照旧,先上java代码:
public class MyLocationMapActivity extends Activity {
    private LocationManager manager;//定位管理器
    private SharedPreferences sp;//存储
    private TextView outputLat,outputLon;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_location_layout);
        outputLat=(TextView) findViewById(R.id.outputLat);
        outputLon=(TextView) findViewById(R.id.outputLon);
        
        manager = (LocationManager) getSystemService(LOCATION_SERVICE);//获取手机位置信息
        
//        List<String> providers = manager.getAllProviders();//获得所有可用的位置提供者
//        for (String provider : providers) {
//            System.out.println(provider);
//        }
        
       //获取的条件
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);//获取精准位置
        criteria.setCostAllowed(true);//允许产生开销
        criteria.setPowerRequirement(Criteria.POWER_HIGH);//消耗大的话,获取的频率高
        criteria.setSpeedRequired(true);//手机位置移动
        criteria.setAltitudeRequired(true);//海拔
        
        //获取最佳provider: 手机或者模拟器上均为gps
        String bestProvider = manager.getBestProvider(criteria, true);//使用GPS卫星
        
        System.out.println("最好的位置提供者是"+bestProvider);
        sp = getSharedPreferences("config",MODE_PRIVATE);
        
        //parameter: 1. provider 2. 每隔多少时间获取一次  3.每隔多少米  4.监听器触发回调函数
        manager.requestLocationUpdates(bestProvider,60000,1, new MyLocationListener());
    }
    
    private class MyLocationListener implements LocationListener{
        /**
         * 手机位置发生变动
         */
        public void onLocationChanged(Location location) {
            location.getAccuracy();//精确度
            String  latitude = location.getLatitude()+"";//经度
            String longitude = location.getLongitude()+"";//纬度
            outputLat.setText("当前经度为:"+latitude);
            outputLon.setText("当前纬度为:"+longitude);
            
            //将获取到的经纬度信息存入SharedPreferences
            Editor editor = sp.edit();
            editor.putString("lastloaction", latitude + "-" + longitude);
            editor.commit();
            
        }
        
        /**
         * 当某个位置提供者的状态发生改变时
         */
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        /**
         * 某个设备打开时
         */
        public void onProviderEnabled(String provider) {
        }
        /**
         * 某个设备关闭时
         */
        public void onProviderDisabled(String provider) {
        }
    }
}

然后才是.xml

<?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="当前经纬度" />

    <TextView
        android:id="@+id/outputLat"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#BFEFFF"
        android:text="Lat" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dip" />

    <TextView
        android:id="@+id/outputLon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#BFEFFF"
        android:text="Lon" />

</LinearLayout>

manifest.xml中的代码还是一样,只是加多了这一行
<activity android:name="com.My.mymaptest.MyLocationMapActivity" />

运行结果就不贴上了。
总之,结果是分别打印出经度和纬度来。
哈哈。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值