【发布时间】:2017-01-31 15:13:42
【问题描述】:
我想再次使用 lat、lon 变量,但是当我在 onConnected() 中设置它时,它并没有在 MainActivity 中设置。 这是我的代码,
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final int PERMISSION_ACCESS_FINE_LOCATION = 1;
private GoogleApiClient googleApiClient;
protected Context context;
double lat, lon;
TextView txtLat;
double latitude = 0, longitude = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_ACCESS_FINE_LOCATION);
}
googleApiClient = new GoogleApiClient.Builder(this, this, this).addApi(LocationServices.API).build();
}
这是另一部分,包括 get lat long。
@Override
public void onConnected(Bundle bundle) {
Log.i(MainActivity.class.getSimpleName(), "Connected to Google Play Services!");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
lat = lastLocation.getLatitude();
lon = lastLocation.getLongitude();
txtLat = (TextView) findViewById(R.id.textView1);
txtLat.setText(lat + " " + lon);
}
}
【问题讨论】:
-
什么
double latitude = 0, longitude = 0;这是你想再次重复使用的吗? -
@ntaloventi 没有。纬度,经度
标签: android google-maps location