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

来源:互联网 发布:加华伟业资本 知乎 编辑:程序博客网 时间:2024/05/22 15:22

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;}@Overridepublic void onLocationChanged(Location location) {        updateLocation(location);}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic 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("无法获取地理信息");        }    }    }

效果如下:



原创粉丝点击