android 原生方法获取定位

package com.example.getlocation;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity
{

   private double latitude=0.0;
   private double longitude =0.0;
   LocationManager locationManager =null ;

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final TextView showText = (TextView)findViewById(R.id.show_text);

      locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
       
      LocationListener locationListener = new LocationListener() {
            
            // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
               
            }
            
            // Providerenable时触发此函数,比如GPS被打开
            @Override
            public void onProviderEnabled(String provider) {
               Log.e("Map", "onProviderEnabled "  );
            }
            
            // Providerdisable时触发此函数,比如GPS被关闭 
            @Override
            public void onProviderDisabled(String provider) {
               
            }
            
            //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 
            @Override
            public void onLocationChanged(Location location) {
               if (location != null) 
               {   
                  Log.e("Map", "Location changed : Lat: "  
                  + location.getLatitude() + " Lng: "  
                  + location.getLongitude());   
                  showText.setText("Latitude:"+ location.getLatitude() +"Longitude"+location.getLongitude()); 
                  stopLister(this);
               }
            }
         };
         
         if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
         {
            //第一个参数,与取
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0,locationListener);   
         }
         else
         {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 0,locationListener); 
         }
  
         //取上一次定位的位置
//       Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
//       if(location != null){   
//          latitude = location.getLatitude(); //经度   
//          longitude = location.getLongitude(); //纬度
//          
//          Log.e("Map", "NETWORK_PROVIDER changed : Lat: "  
//          + location.getLatitude() + " Lng: "  
//          + location.getLongitude());   
//       }
         
   }

   /**
    * 销毁定位
    */
   private void stopLister(LocationListener listener) {
      if (locationManager != null) {
         locationManager.removeUpdates(listener);
      }
      locationManager = null;
   }
}

转载于:https://my.oschina.net/u/588516/blog/816214

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值