我正在努力獲取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始終顯示當前位置
標記確實顯示模擬位置,但onLocation始終顯示當前位置 - 那麼究竟是什麼問題? – user2450263
問題是當前位置顯示的是真實的當前位置,而不是由http://developer.android.com/training/location/location-testing.html提供的測試應用程序提供的位置。我可以看到當前標記在不同的模擬位置上移動,但onLocationChanged總是顯示我的真實位置,而不是模擬位置。 – prap19