我正在尝试获取位置经度和纬度,以便在更新时出现在祝酒词上。但是,没有出现!地图加载得很好,但我无法获得吐司中的坐标。任何提示?
package com.parse.starter;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
public class ParseStarterProjectActivity extends Activity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
/** Called when the activity is first created. */
/**
* Provides the entry point to Google Play services.
*/
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 20000;
/**
* The fastest rate for active location updates. Exact. Updates will never be more frequent
* than this value.
*/
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
// Keys for storing activity state in the Bundle.
protected final static String REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key";
protected final static String LOCATION_KEY = "location-key";
protected final static String LAST_UPDATED_TIME_STRING_KEY = "last-updated-time-string-key";
protected GoogleApiClient mGoogleApiClient;
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
protected LocationRequest mLocationRequest;
/**
* Represents a geographical location.
*/
protected Location mCurrentLocation;
/**
* Tracks the status of the location updates request. Value changes when the user presses the
* Start Updates and Stop Updates buttons.
*/
protected Boolean mRequestingLocationUpdates;
/**
* Time when the location was updated represented as a String.
*/
protected String mLastUpdateTime;
public void onCreate(Bundle savedInstanceState) {
Log.d("message: ", "1");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
Log.d("message: ", "made it this far");
try {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception exc) {
Log.d("ERror:", exc.getMessage().toString());
}
buildGoogleApiClient();
}
@Override
public void onMapReady(GoogleMap map) {
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
map.setMyLocationEnabled(true);
}
protected synchronized void buildGoogleApiClient() {
Toast.makeText(this,"buildGoogleApiClient",
Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this,"onConnected",
Toast.LENGTH_SHORT).show();
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
Toast.makeText(this,"startLocationUpdates",
Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onLocationChanged(Location location) {
// mCurrentLocation = location;
// location.
mCurrentLocation=location;
TextView txt = (TextView) findViewById(R.id.longlat);
Toast.makeText(this,location.getLongitude() + " " + location.getLatitude() ,
Toast.LENGTH_SHORT).show();
Toast.makeText(this,"onLocationChanged",
Toast.LENGTH_SHORT).show();
Log.i("longitude / latitude", location.getLongitude() + " " + location.getLatitude());
ParseObject sLocation = new ParseObject("Location");
sLocation.put("longitude", location.getLongitude());
sLocation.put("latitude", location.getLatitude());
sLocation.put("altitude", location.getAltitude());
sLocation.put("accuracy", location.getAccuracy());
sLocation.saveInBackground();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}