没有调用onLocationChanged来检索位置

时间:2014-09-06 20:23:00

标签: android google-maps

我正在开发一个使用google maps api的应用程序,现在它可以很好地用于我用来测试应用程序的真实设备。几天前我在平板电脑上进行了硬重置,当我尝试再次测试我的应用程序,它没有像以前那样工作。所以在我的logcat中,我看到locationClient已连接,但它无法识别我的位置,因此永远不会调用onLocationChanged。< / p>

我注意到我的logcat:

I/Google Maps Android API(2924): Google Play services client version: 5089000
I/Google Maps Android API(2924): Google Play services package version: 5089032

我知道这是两个不同版本的谷歌播放服务,我无法理解如何修复它,因为在平板电脑的硬重置之前,我没有将此信息导入logcat。

这是我的manifest.xml:

?xml version="1.0" encoding="utf-8"?>
manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.logic.main"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.logic.main.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.logic.main.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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_MOCK_LOCATION" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.MyAppTheme" >
        <activity
            android:name="com.logic.main.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            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="com.logic.maps.activity.MapsActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name" >
        </activity>

        <service
            android:name="com.logic.maps.backgroundService.LocationBackgroundService"
            android:enabled="true" >
        </service>

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

</manifest>

活性:

public class MapsActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener,
        LocationListener, OnInfoWindowClickListener, OnMarkerClickListener {

    private static final long UPDATE_INTERVAL = 5000;
    private static final long FASTEST_INTERVAL = 1000;

    private GoogleMap map = null;
    private LocationClient locationClient;
    private Location myLocation;
    private LocationRequest locationRequest;
    private LatLng newPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        locationClient = new LocationClient(this, this, this);
        locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(UPDATE_INTERVAL)
                .setFastestInterval(FASTEST_INTERVAL).setSmallestDisplacement(10);
        if (locationClient != null)
            // connect the client to the Google Play services
            locationClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        //THIS IS NEVER CALLED
        newPosition = new LatLng(location.getLatitude(), location.getLongitude());
        myLocation = location;
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(newPosition, 15));
    }

    @Override
    public void onConnected(Bundle arg0) {
        //*************************** AT THE BEGINNING THIS IS CALLED***************/
        // BUT onLocationChanged is never called
        Toast.makeText(this, "I'm bringing you to your area.", Toast.LENGTH_SHORT).show();
        locationClient.requestLocationUpdates(locationRequest, this);
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
    }

    [...]
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

从Android SDK管理器安装最新的Google Play服务

答案 1 :(得分:1)

启用位置服务:

settings-&gt; location and security-&gt;位于我的位置 - &gt;选择使用无线并使用gps。

答案 2 :(得分:0)

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();

// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location
locationManager.requestLocationUpdates(provider, 10000L, 1f, (LocationListener) this);