2012-05-04 22 views
0

私は次のコードを試してみましたが、現在の位置を取得していません。
私は手動でエミュレータコントロールに位置を設定すると、そのような場所は取得されますが、現在の場所は取得されません。アンドロイドの現在の位置を取得

  • 現在の場所の取得方法は?
  • 現在の場所を取得する他の方法はありますか?

これは私のコードです:

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); 
} 
関連する問題