现在的位置: 首页 > 移动开发 > 正文

Android Location在GPS中的应用(一) .

2018年09月28日 移动开发 ⁄ 共 4289字 ⁄ 字号 评论关闭

原文地址:http://blog.csdn.net/kmyhy/article/details/6276680

新建AndroidProject,注意选择Google APIs:

 

打开AndroidManifest.xml,在其中加入GPS使用权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"/>

 

main.java的代码如下:

publicclass main
extends Activity {

    /** Called when the activity is first created. */

private LocationManager
locationManager;

private String
provider;

private Location
location;

private Address
address;

    @Override

    publicvoid onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //
获取LocationManager服务

        locationManager =(LocationManager)
this

               .getSystemService(Context.LOCATION_SERVICE);

        //
获取Location Provider

        getProvider();

        //
如果未设置位置源,打开GPS设置界面

        openGPS();

        //
获取位置

        location =
locationManager.getLastKnownLocation(provider);

        //
显示位置信息到文字标签

        updateWithNewLocation(location);

        //
注册监听器locationListener,第23个参数可以控制接收gps消息的频度以节省电力。第2个参数为毫秒,

        //
表示调用listener的周期,第3个参数为米,表示位置移动指定距离后就调用listener

        locationManager.requestLocationUpdates(provider, 2000, 10,

                       locationListener);

    }

    // 判断是否开启GPS,若未开启,打开GPS设置界面

    privatevoid openGPS() {       

        if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)

        ||locationManager.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)

        ) {

           Toast.makeText(this,
"位置源已设置!", Toast.LENGTH_SHORT).show();

            return;

        }

        Toast.makeText(this,
"位置源未设置!", Toast.LENGTH_SHORT).show();

        //
转至GPS设置界面

        Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

       startActivityForResult(intent,0);

    }

    // 获取Location Provider

    privatevoid getProvider(){

    // 构建位置查询条件

        Criteria criteria = new Criteria();

        //
查询精度:高

       criteria.setAccuracy(Criteria.ACCURACY_FINE);

        //
是否查询海拨:否

       criteria.setAltitudeRequired(false);

        //
是否查询方位角:

       criteria.setBearingRequired(false);

        //
是否允许付费:是

        criteria.setCostAllowed(true);

        //
电量要求:低

       criteria.setPowerRequirement(Criteria.POWER_LOW);

        //
返回最合适的符合条件的provider,第2个参数为true说明,如果只有一个provider是有效的,则返回当前provider

        provider =
locationManager.getBestProvider(criteria,true);  

    }

 // Gps消息监听器

    privatefinal LocationListener
locationListener =
new LocationListener(){

    // 位置发生改变后调用

        publicvoidonLocationChanged(Location location) {

       updateWithNewLocation(location);

        }

        // provider被用户关闭后调用

        publicvoidonProviderDisabled(String provider){

        updateWithNewLocation(null);

        }

        // provider被用户开启后调用

        publicvoidonProviderEnabled(String provider){ }

        // provider状态变化时调用

        publicvoidonStatusChanged(String provider,
int status,

        Bundle extras){ }

    };

    // Gps监听器调用,处理位置信息

    privatevoid updateWithNewLocation(Locationlocation) {

        String latLongString;

        TextView myLocationText= (TextView)findViewById(R.id.text);

        if (location !=
null) {

        double lat =location.getLatitude();

        double lng =location.getLongitude();

        latLongString =
"
纬度:" + lat +
"/n经度:" + lng;

        } else {

        latLongString =
"
无法获取地理信息";

        }

        myLocationText.setText("您当前的位置是:/n" +

        latLongString+"/n"+getAddressbyGeoPoint(location));

    }

// 获取地址信息

privateList<Address> getAddressbyGeoPoint(Location location) {

List<Address> result = null;

// 先将Location转换为GeoPoint

// GeoPoint gp=getGeoByLocation(location);

try {

if (location !=
null) {

// 获取Geocoder,通过Geocoder就可以拿到地址信息

Geocoder gc = new Geocoder(this, Locale.getDefault());

result= gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

}

如果,要想在模拟器中看到效果,还需要在DDMS的EmulatorControl面板中进行一些设置。如果你看不到Emulator Control面板,可以从window->Show view->Other…中打开它:

 

在Emulator Control面板中,你可以找到一个LocationControls的地方:

 

你可以在Longitude和Latitude中输入一个虚拟的经纬度,然后点击Send,可以把模拟器的位置修改为指定位置。此时运行程序,即可看到如图所示的信息:

 

有时候,你可能出现“无法获取地理信息”的错误。这可能是没有开启“启用GPS卫星”选项。不要奇怪,在模拟器中,“使用无线网络”是无效的,使用该选项无法获取地理信息,因为模拟器中根本没有sim卡,也就无法通过基站来定位了。

如果在真机上就不同了。如果机器没有内置GPS模块,那么启用GPS卫星选项反而无法进行定位,具体情况只有多试几次才能明白。

@font-face { font-family: "宋体";}@font-face { font-family: "Cambria";}p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }div.Section1 { page: Section1; }

Geocoder需要访问internet,在真机上调试请打开wifi或者3G网络。

 

抱歉!评论已关闭.