2017-05-04 62 views
0

在我的应用程序中,我需要本地化服务。我使用的是Google服务,因为我发现它们比Android更好,更精确。地点经理。我观看了不同的Tutos并尝试了不同的代码,但它不适用于我的模拟器或手机。 Main2Activity:我的使用Google Play服务的本地化不起作用

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> 
+0

你得到了什么错误? – abielita

+0

在我的LogCat中,从我的Log.i我只得到了代码中提到的位置失败,否则没有其他提到 – Bayram

回答

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测试。

+0

Infortunally它不起作用,我得到这个消息:“我/ Main2Activity:位置失败” – Bayram

相关问题