`
张洪财
  • 浏览: 267193 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

获得位置信息 android

阅读更多

package ziv.locationdemo;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LocationDemo extends Activity {
 TextView locationShow = null;        // 显示区
 TextView infoTextView = null;        // 提示信息显示
 Button locationbutton = null;        // 绑定GPS监听按钮
 Button locationbuttonNetwork=null;       // 绑定NEWWOR简体按钮
 LocationManager locationManager = null;     // 定位管理
 public LocationListener locationListener_GPS = null;  // 监听gps返回数据
 public LocationListener locationListener_NETWORK=null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 实例化显示区
  locationShow = (TextView) findViewById(R.id.locationShow);
  // 在视图层提出提示信息文本框
  infoTextView = (TextView) findViewById(R.id.infoTextView);
  // 实例化绑定监听按钮
  locationbutton = (Button) findViewById(R.id.locationbutton);
  locationbuttonNetwork = (Button) findViewById(R.id.locationbuttonNetwork);
  
  // 获得定位管理类
  locationManager = (LocationManager) LocationDemo.this
    .getSystemService(Context.LOCATION_SERVICE);
  // 实例化监听对象变量
  locationListener_GPS = new LocationListenerSelf("GPS");
  locationListener_NETWORK=new LocationListenerSelf("NETWORK");
  locationbuttonNetwork.setOnClickListener(new OnClickListenerButton(LocationManager.NETWORK_PROVIDER));
  // 监听单击按钮
  locationbutton.setOnClickListener(new OnClickListenerButton(LocationManager.GPS_PROVIDER));

 }

 // 执行就绪
 @Override
 protected void onResume() {
  // 在就绪执行中,自动选择位置信息提供者,且触发监听
  if (locationManager != null) {

   // 实例化查询条件
   Criteria criteria = new Criteria();
   criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
   // 获得定位服务提供者名称
   String provider = locationManager.getBestProvider(criteria, true);
   
   //locationManager.setTestProviderEnabled("gps", true);
   //criteria.setCostAllowed(true);

   // 自动位置变化监听
   if("gps".equals(provider)){
    
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_NETWORK);
    
   }
   
   // 获得最后的位置信息
   Location location = locationManager.getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" onResume 没获得到定位\n");
    infoTextView.setText("提供者:"+provider);
   }
  }
  super.onResume();
 }

 /**
  * 显示location 信息
  *
  * @param location
  */
 private void showLocation(Location location,String provider) {

  locationShow.append("经度:" + location.getLongitude() + "\n" + "纬度:"
    + location.getLatitude() + "\n");
  infoTextView.setText("提供者:"+provider);

 }
 
 @Override
 protected void onPause() {
  System.out.println("onPause");
  if (locationManager != null) {
   locationManager.removeUpdates(locationListener_GPS);
   locationManager.removeUpdates(locationListener_NETWORK);
  }
  super.onPause();
 }
 private class OnClickListenerButton implements OnClickListener{
  String provider="";
  public OnClickListenerButton(){
   super();
  }
  public OnClickListenerButton(String provider){
   super();
   this.provider=provider;
  }
  @Override
  public void onClick(View v) {
 
   Location location = locationManager
     .getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" 没获得到定位\n");
    infoTextView.setText("提供者:"+provider+";是否可用:"+locationManager.isProviderEnabled(provider));
   }
   //启动gps监听
   if(locationManager.GPS_PROVIDER.equals(provider)){
    
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_NETWORK);
   }
   
  }

 
 }
 /**
  * 位置变化监听
  * @author ziv
  *
  */
 private class LocationListenerSelf implements LocationListener{
  String provider="null";
  public LocationListenerSelf(){
   
  }
  public LocationListenerSelf(String provider){
   this.provider=provider;
  }
  @Override
  public void onLocationChanged(Location location) {
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" location listener 没获得到定位 \n");
    infoTextView.setText("提供者:"+provider);
   }
  }

  @Override
  public void onProviderDisabled(String provider) {
   System.out.println("onProviderDisabled");

  }

  @Override
  public void onProviderEnabled(String provider) {
   System.out.println("onProviderEnabled");

  }

  @Override
  public void onStatusChanged(String provider, int status,
    Bundle extras) {
   System.out.println("onStatusChanged");

  }

 
 }
}

 

 

成功执行 。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics