2014-06-17 41 views
2

我正在努力獲取Android設備的當前位置不斷更新的位置api。我在這裏做錯了什麼?Android位置API不顯示模擬位置onLocationChanged

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.widget.Toast; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.GoogleMapOptions; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 

public class ViewMapActivity extends FragmentActivity implements LocationListener, 
     GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { 

    public static final Long LOCATION_MIN_TIME = 1000L; 
    public static final float LOCATION_MIN_DISTANCE = 10f; 

    private static GoogleMap googleMap; 
    private LocationManager locationManager; 
    private Criteria criteria; 
    private String provider; 


    public ViewMapActivity() { 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.i("ViewMap"," Map created"); 
     setContentView(R.layout.map); 
     GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
     GoogleMapOptions mapOptions = new GoogleMapOptions(); 
     mapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL); 
     mapOptions.zoomGesturesEnabled(true); 
     mapOptions.zoomControlsEnabled(true); 
     googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment)) 
       .getMap(); 
     googleMap.setMyLocationEnabled(true); 

     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     //Log.d("ViewMap", provider.toString()); 

     Location location = locationManager.getLastKnownLocation(provider); 

     if(location != null) { 
      onLocationChanged(location); 
     } else { 
      Log.d("ViewMap", "Location found onCreate."+location.toString()); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.d("ViewMap", "Paused"); 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d("ViewMap", "Resume"); 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.i("ViewMap", "location changed"); 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     //googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     //googleMap.animateCamera(CameraUpdateFactory.zoomBy(15)); //TODO: move this to static config 
     String loc = "Lat:"+latLng.latitude+" long:"+latLng.longitude; 
     Toast.makeText(getBaseContext(), loc, Toast.LENGTH_SHORT).show(); 
     Log.d("ViewMap", latLng.toString()); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 
     Log.d("ViewMap", "Status Changed: "+s); 
    } 

    @Override 
    public void onProviderEnabled(String s) { 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      Log.d("ViewMap", "GPS Provider"); 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this); 
     } else { 
      Log.d("ViewMap", "Network Provider"); 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String s) { 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     } else { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     // TODO may be add some logs 
     Log.i("ViewMap", "GPS can work now"); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); 
    } 

    @Override 
    public void onDisconnected() { 
     // TODO may be add some logs 
     Log.i("ViewMap", "GPS is disconnected"); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     // TODO may be add some logs 
     Log.e("ViewMap", "Google play connection failed. GPS might not work"); 
    } 
} 

這裏是AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.sample.activity" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="18"/> 

    <permission 
      android:name="com.sample.activity.permissions.MAP_RECEIVE" 
      android:protectionLevel="signature" /> 

    <uses-feature 
      android:glEsVersion="0x00020000" 
      android:required="true" /> 

    <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission 
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" 
/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 


    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
     <activity android:name="SplashScreenStartActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 

     <activity android:name=".ViewMapActivity"/> 



     <meta-data android:name="com.google.android.maps.v2.API_KEY" 
android:value="@string/google_map_api_key" /> 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
    </application> </manifest> 

作爲後續行動的問題,我如何與通過模擬地點測試這一活動。我正在使用來自http://developer.android.com/training/location/location-testing.html的模擬位置示例。標記確實顯示模擬位置,但onLocation始終顯示當前位置

+0

標記確實顯示模擬位置,但onLocation始終顯示當前位置 - 那麼究竟是什麼問題? – user2450263

+0

問題是當前位置顯示的是真實的當前位置,而不是由http://developer.android.com/training/location/location-testing.html提供的測試應用程序提供的位置。我可以看到當前標記在不同的模擬位置上移動,但onLocationChanged總是顯示我的真實位置,而不是模擬位置。 – prap19

回答

0

您是否可以使用您的活動的導入集更新問題,如果其不正確,則會編輯/刪除我的答案。
爲什麼你總是看到當前位置可能是因爲googleMap.setMyLocationEnabled(true);onCreate()

坦率地說,有問題的代碼似乎來自多個來源的複製粘貼沒有理解太多或細節都被忽略了,沒有那麼溫和。

代碼包含兩種獲取用戶位置的方法。
1.使用LocationClientRetrieving the Current Location
這實現從GooglePlayServicesClient
接口包是com.google.android.gms.location.LocationClient所以其本LocationListener.
Testing Using Mock Locations鏈路,這一個已被使用。

2.使用LocationManager
與你使用的方法requestLocationUpdates()。在這裏,包是android.location所以它的這個LocationListener.

這意味着你的代碼是什麼:
1.1您已經實現了第一種方法監聽器,但你永遠不設置,來電或使用locationClient.connect()所以下面的代碼部分永遠不會被調用。

@Override 
    public void onConnected(Bundle bundle) { 
     // TODO may be add some logs 
     Log.i("ViewMap", "GPS can work now"); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); 
    } 

    @Override 
    public void onDisconnected() { 
     // TODO may be add some logs 
     Log.i("ViewMap", "GPS is disconnected"); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     // TODO may be add some logs 
     Log.e("ViewMap", "Google play connection failed. GPS might not work"); 
    } 

無論如何,您應該使用此方法,因爲您仍在使用Google Play服務。

2.1locationManager.requestLocationUpdates(provider, 400, 1, this);
這是什麼讓你的位置,如果你已經導入正確的LocationListener的。你可以檢查進口,如果它不是android.location包,我不認爲代碼到達那裏。

+0

增加了一些導入語句。是的你是對的,代碼只是複製粘貼來自不同來源的代碼。我對這些概念很陌生。 – prap19

3

模擬位置僅適用於GPS,它不會僞造任何其他提供商。具體來說,它不會僞造NETWORK位置提供商。不要使用標準選擇最佳提供商,請指定GPS提供商。

通常我不認爲標準是有用的。你寫了你的應用程序,你知道你是否需要GPS的準確性,或者想要通過網絡節省電量。指定你想要的,而不是讓OS爲你挑選一個。

+0

你的意思是,如果得到GPS_PROVIDER,我將能夠聽取模擬位置。我想我早些時候嘗試過,但沒有奏效。讓我再試一次,我會讓你知道。 – prap19