1

我试过了一个应用程序来显示经纬度使用谷歌播放服务,它的工作罚款低于棒棒糖版本。但它显示棒棒糖上的“未检测到位置”。我用下面的代码:经纬度没有得到棒棒糖版本使用谷歌播放服务

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private GoogleApiClient mGoogleApiClient; 
private Location mLocation; 
private static final String TAG = "MainActivity"; 
private TextView mLatitudeTextView; 
private TextView mLongitudeTextView; 
Geocoder geocoder; 
List<Address> addresses; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview)); 
    mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview)); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

} 

@Override 
public void onConnected(Bundle bundle) { 

    mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (mLocation != null) { 
    mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude())); 
      mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude())); 

    } else { 
     Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); 
    } 
} 

@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 
protected void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 
} 

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

我列入清单文件编译gradle这个com.google.android.gms:play-services:8.1.0android.permission.ACCESS_COARSE_LOCATION

请有人帮我解决这个问题。

回答

0

尝试添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果使用本教程不尝试:http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

+0

确保一切都在正确包括您的清单文件。 – Lotus91

+0

我试过这个教程http://www.sitepoint.com/google-play-services-location-activity-recognition/。即Fine Location使用设备GPS,蜂窝数据和WiFi来获得最准确的位置,但这会耗费电池寿命。粗略位置使用设备蜂窝数据和WiFi来获取位置。它不会像精细一样精确,但使用更少的电池电量,以相当于城市街区的精度返回一个位置。 – jasil

0

使用此方法连接到谷歌API客户端:

GoogleApiClient apiClient=null; 
LocationRequest mLocationRequest=null; 

private void setLocationLocationRequest() { 

     try { 
      apiClient=new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 

      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(29000); 
      mLocationRequest.setFastestInterval(5000); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
      apiClient.connect(); 

     }catch (Exception e){ 
      Log.d("LS", e.getMessage() == null ? "" : e.getMessage()); 
     } 

    } 

欲了解更多详细信息,请this Answerthis answer