Android入门:使用Android GPS实现简单的定位

Activity:

 

package com.van.gps;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

public class GPSTestActivity extends Activity {
	
	
	private TextView textView;//显示文本框
	private LocationManager locationManager;//位置管理
	private GPSLocationListener locationListener;//位置监听器
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView) this.findViewById(R.id.textView_location);
        locationListener=new GPSLocationListener(textView);
        
        //首先打开GPS,查找位置。
        openGPSSettings();
    }
    

    /**
     * 设置GPS。
     */
    private void openGPSSettings() {
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        
        if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            getLocation();
            return;
        }

        
        
        //提示用户打开GPS
        AlertDialog.Builder builder = new Builder(GPSTestActivity.this); 
        builder.setMessage("必须要开启GPS才能使用此程序,开启?"); 
        builder.setTitle("提示"); 
        builder.setPositiveButton("确认", 
                new android.content.DialogInterface.OnClickListener() { 
                    
                    public void onClick(DialogInterface dialog, int which) { 
                    	Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(intent,0); //此为设置完成后返回到获取界面
                    } 
                }); 
        		builder.setNegativeButton("退出", 
                new android.content.DialogInterface.OnClickListener() { 
                 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        GPSTestActivity.this.finish();
                        
                    } 
                }); 
        builder.create().show(); 
        
    }
    

    /**
     * 获取地理位置。
     */
    private void getLocation(){
    	
	    // 查找到服务信息
	    Criteria criteria = new Criteria();
	    criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
	    criteria.setAltitudeRequired(false);
	    criteria.setBearingRequired(false);
	    criteria.setCostAllowed(true);
	    criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

	    
	    /**
	     * ANDROID中有两种获取位置的方式,LocationManager.NETWORK_PROVIDER和LocationManager.GPS_PROVIDER;
	     * 前者用于移动网络中获取位置,精度较低但速度很快, 后者使用GPS进行定位,精度很高但一般需要10-60秒时
	     * 间才能开始第1次定位,如果是在 室内则基本上无法定位。
	     * 此方法使用Criteria得到最佳的方式
	     */
	    
	    String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
	    Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
	    
	    locationListener.updateLocation(location);//调用方法,更新位置信息
	    
	    // 设置监听器,1秒监听一次
	    locationManager.requestLocationUpdates(provider, 1000, 0 ,locationListener);
	    
    }


GPSLocationListener:

 

 

package com.van.gps;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;

public class GPSLocationListener implements LocationListener{

	//显示文本
	private TextView textView;
	
	
	/**
	 * 构造.
	 * @param textView
	 */
	public GPSLocationListener(TextView textView){
		this.textView=textView;
	}
	
	@Override
	public void onLocationChanged(Location location) {
	        updateLocation(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	/**
     * 更新位置显示.
     * @param location
     */
    public void updateLocation(Location location) {

        
        if (location != null) {
            double  latitude = location.getLatitude();
            double longitude= location.getLongitude();
            textView.setText("维度:" +  latitude+ "\n经度:" + longitude);
        } else {
        	textView.setText("无法获取地理信息");
        }

    }

    

}

 

 

效果如下:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值