我使用Google Play服务进行本地化无效

时间:2017-05-04 09:50:24

标签: android google-play-services

在我的应用中,我需要本地化服务。我正在使用Google服务,因为我发现它们比Android更好,更精确。位置经理。我看了不同的Tutos并尝试了不同的代码,但它既不是在我的模拟器也不是在我的手机中工作。 Main2Activity:

public class Main2Activity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

private TextView textView;
private Button btn_localisation;
private Button btn_update_Localisation;
private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private LocationRequest mLocationRequest;


private boolean mRequestingLocationUpdates = false;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;


// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FATEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 10; // 10 meters
private static final String TAG = Main2Activity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    textView = (TextView) findViewById(R.id.textview_localisation);
    textView.setText("Waiting for updates");
    btn_localisation = (Button) findViewById(R.id.btn_localisation);
    btn_update_Localisation = (Button) findViewById(R.id.btn_update_localisation);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
    mLocationRequest.setInterval(5000);
    mLocationRequest.setFastestInterval(3000);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

}

    @Override
    public void onConnectionSuspended ( int i){
        Log.i(TAG, "Connection Suspended");
        mGoogleApiClient.connect();
    }


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}

@Override
public void onLocationChanged(Location location) {
    textView.setText(String.valueOf(location.getLatitude())+","+location.getLongitude());
    Toast.makeText(this, "Updated: ", Toast.LENGTH_SHORT).show();

}

}

Gradle.build:

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.google.android.gms:play-services-maps:10.0.1'
compile 'com.google.android.gms:play-services-location:10.0.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:10.2.4'

的Manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bayram.com.databasetutorial">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ShowData" />
    <activity android:name=".Add" />




    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name=".Main2Activity"></activity>
</application>

</manifest>

2 个答案:

答案 0 :(得分:0)

替换此代码:

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
    mLocationRequest.setInterval(5000);
    mLocationRequest.setFastestInterval(3000);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

}

用这个:

@Override
public void onConnected(@Nullable Bundle bundle) {



    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

            if (mLastLocation != null) {
                mLatitude =  mLastLocation.getLatitude();
                mLongitude = mLastLocation.getLongitude();
                Log.i(TAG, "Location"+mLatitude+"long"+mLongitude);

            }
            else {
                Log.i(TAG, "Location failed");

            }
        }

尝试不添加checkSelfPermission(它将要求您避免它),测试并且如果它正确运行则添加checkSelfPermission测试。

答案 1 :(得分:0)

此代码运行正常,唯一要做的就是授予应用程序访问电话位置的权限。 做那个设置 - &gt;应用程序 - &gt;你的应用 - &gt;权限,并激活该位置的权限。