android网络-GPS获取定位信息
2012-05-01 17:16
483 查看
设置每60秒,每移动十米向LocationProvider获取一次GPS的定位信息
当LocationProvider可用,不可用或定位信息改变时,调用updateView,更新显示
程序效果:按下按钮后,按要求更新定位信息的显示
DDMS的Emulator Control面板中Manual输入经度和纬度值,单击“send”,即可向模拟器发出GPS定位信息(模拟手机中GPS开启状态下自动获取定位信息)
先加上两个权限,第二个为获取定位信息的权限
主activity
当LocationProvider可用,不可用或定位信息改变时,调用updateView,更新显示
程序效果:按下按钮后,按要求更新定位信息的显示
DDMS的Emulator Control面板中Manual输入经度和纬度值,单击“send”,即可向模拟器发出GPS定位信息(模拟手机中GPS开启状态下自动获取定位信息)
先加上两个权限,第二个为获取定位信息的权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
主activity
package com.song; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class C613_Googlemap2Activity extends Activity { /** Called when the activity is first created. */ Button button; TextView textview; LocationManager manager; Location location; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textview=(TextView)findViewById(R.id.textview); button=(Button)findViewById(R.id.button); manager=(LocationManager)getSystemService(LOCATION_SERVICE); //从GPS_PROVIDER获取最近的定位信息 location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); updateView(location); //判断GPS是否可用 System.out.println("state="+manager.isProviderEnabled(LocationManager.GPS_PROVIDER)); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //设置每60秒,每移动十米向LocationProvider获取一次GPS的定位信息 //当LocationProvider可用,不可用或定位信息改变时,调用updateView,更新显示 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub // updateView(manager.getLastKnownLocation(provider)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub updateView(null); } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub //location为变化完的新位置,更新显示 updateView(location); } }); } }); } //更新显示内容的方法 public void updateView(Location location) { StringBuffer buffer=new StringBuffer(); if(location==null) { textview.setText("未获得服务"); return; } buffer.append("经度:"+location.getLongitude()+"\n"); buffer.append("纬度:"+location.getLatitude()+"\n"); buffer.append("高度:"+location.getAltitude()+"\n"); buffer.append("速度:"+location.getSpeed()+"\n"); buffer.append("方向:"+location.getBearing()+"\n"); textview.setText(buffer.toString()); } }显示效果
相关文章推荐
- Android 获取GPS和网络定位信息
- android之GPS或网络获取经纬度信息
- android GSM+CDMA基站定位--获取移动网络相关信息
- Android中使用GPS和NetWork获取定位信息
- 使用Android原生的Api进行GPS定位获取位置信息
- Android获取当前位置(GPS和网络定位)
- Android获取位置信息 - GPS定位 Network定位
- Android开发,GPS获取实时时间并转为北京时间,定位信息,海拔高度,并进行显示
- Android中使用GPS和NetWork获取定位信息
- Android获取当前位置(GPS和网络定位)
- Android 打开 GPS 导航并获取位置信息
- Android GPS定位 获取经纬度
- android中利用GPS获取位置信息的源码分析
- Android最简单的单例模式,基于百度地图随时获取定位信息
- Android基站定位——通过手机信号获取基站信息(一)
- Android初级教程获取手机位置信息GPS与动态获取最佳方式
- Android网络编程---从服务器获取文本信息
- Android打开GPS导航并获取位置信息返回null解决方案
- android使用百度地图SDK获取定位信息示例