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;
}
}
相關文章
相關標籤/搜索