2012-05-04 207 views
0

我試過下面的代碼,但沒有獲取當前位置。
當我手動設置模擬器控制位置,然後我得到這樣的位置,但沒有得到當前位置。我得到空位置。在android中獲取當前位置

  • 如何獲取當前位置?
  • 有沒有其他方法可以獲得當前位置?

這是我的代碼:

package com.p; 
import android.app.Activity; 
import android.os.Bundle; 

import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 

import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
    public class GooglemapActivity extends Activity implements LocationListener { 
    private TextView latituteField; 
    private TextView longitudeField; 
    private LocationManager locationManager; 
    private String provider; 
     EditText t; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



     latituteField = (TextView) findViewById(R.id.TextView02); 
     longitudeField = (TextView) findViewById(R.id.TextView04); 

     // Get the location manager 
    locationManager =     (LocationManager)getSystemService(Context.LOCATION_SERVICE); 



    // Define the criteria how to select the locatioin provider -> use 
    // default 

      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      criteria.setAltitudeRequired(false);//true if required 
      criteria.setBearingRequired(false);//true if required 
      criteria.setCostAllowed(true); 
      criteria.setPowerRequirement(Criteria.POWER_LOW); 
      provider = locationManager.getBestProvider(criteria, true); 
      //provider=LocationManager.GPS_PROVIDER; 
     Location location = locationManager.getLastKnownLocation(provider); 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      /*check provder*/boolean statusOfGPS =locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


      if (statusOfGPS==true) { 
     // Initialize the location fields 
     if (location != null) { 
       System.out.println("Provider " + provider + " has been  selected."); 
        double lat = (double) (location.getLatitude()); 
        double lng = (double) (location.getLongitude()); 
        latituteField.setText(Double.toString(lat)); 
        longitudeField.setText(Double.toString(lng)); 
       } else { 
         latituteField.setText("Provider is not available"); 
         longitudeField.setText("Provider not available"); 
       } 


     } 
     else 
     { 

     latituteField.setText("eeeeeeeee"); 
     longitudeField.setText("rrrrrrrrr"); 

     } 
    } 

    /* Request updates at startup */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 

    /* Remove the locationlistener updates when Activity is paused */ 
    @Override 
    protected void onPause() { 
     super.onPause(); 
    locationManager.removeUpdates(this); 
    } 

    public void onLocationChanged(Location location) { 
      double lat = (double) (location.getLatitude()); 
     double lng = (double) (location.getLongitude()); 
     latituteField.setText(Double.toString(lat)); 
     longitudeField.setText(Double.toString(lng)); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

    public void onProviderEnabled(String provider) { 
     Toast.makeText(this, "Enabled new provider " + provider, 
      Toast.LENGTH_SHORT).show(); 

    } 

    public void onProviderDisabled(String provider) { 
     Toast.makeText(this, "Disabled provider " + provider, 
      Toast.LENGTH_SHORT).show(); 
    } 
    } 

我在下面的manifest.xml添加權限:

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

回答

0

模擬器中你不會得到當前的位置。你可以通過DDMS來設置它們,如果是模擬器,它們將成爲你當前的位置。

+0

我得到的位置由我輸入,但仍然沒有得到當前位置 –

+0

表示您接受來自telnet或模擬器控制的經度和緯度?或者是其他東西? –

0

請不要試圖獲取最後一次知道的位置,大部分時間它是陳舊和無用的。改爲設置locationListener並等待位置更新。這會讓你獲得你當前的位置。

您可以使用此代碼開始列出GPS更新,並對您收到的數據進行處理。它還在日誌文件中打印消息以便於調試。請注意,此功能不會返回任何結果,它只會開始收聽GPS。結果將在稍後的// do something here with the new location data部分提供,屆時您可以將它們保存在某個地方。

private void getGpsData() { 
    locationManager = (LocationManager) owner.getSystemService(Context.LOCATION_SERVICE); 

    locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      Log.i(TAG, "location change:" + location.toString()); 

      String longitude = "Londitude: " + location.getLongitude(); 
      String latitude = "Latitude: " + location.getLatitude(); 

      if(location.hasAccuracy()) { // good enough? 
       // do something here with the new location data 
       .... 
       // 
       Log.i(TAG, "GPS listener done"); 
       locationManager.removeUpdates(this); // don't forget this to save battery 
      } 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Log.i(TAG, provider + " status:" + status); 
     } 

     public void onProviderEnabled(String provider) { 
      Log.i(TAG, provider + " enabled"); 
     } 

     public void onProviderDisabled(String provider) { 
      Log.i(TAG, provider + " disabled"); 
     } 
    }; 
    Log.i(TAG,"GPS listener started"); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
}