您需要implemente位置服務!
使用LocationListener的和GoogleApiClient接口
public class RainstormMap extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 1000;
/**
* Map Settings
*/
private static final int DEFAULT_ZOOM = 15;
private static final int LOCATION_INTERVAL = 1000;
/**
* Map Properties and Utilities
*/
private GoogleMap mMap;
private GoogleApiClient mApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rainstorm_map);
mApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(LOCATION_INTERVAL) // 10 seconds, in milliseconds
.setFastestInterval(LOCATION_INTERVAL); // 1 second, in milliseconds
// setup map
((MapFragment) getFragmentManager().findFragmentById(R.id.googleMap)).getMapAsync(this);
}
/**
* When get User Geo point call this method to list places by location
*
* @param location
* User current location
*/
private void handleNewLocation(Location location) {
// go to user location
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(),
location.getLongitude()),
DEFAULT_ZOOM
));
mMap.addMarker(new MarkerOptions()
.title("Marker")
.position(new LatLng(
location.getLatitude(),
location.getLongitude()));
}
/**
* --------- Google API Connection and get Map if is ready ---------
*/
@Override
public void onConnected(Bundle bundle) {
Location loc = LocationServices.FusedLocationApi.getLastLocation(mApiClient);
if (loc != null) {
handleNewLocation(loc);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(Const.TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
public void onConnectionSuspended(int i) {
//TODO connection suspended implement
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
}
/**
* --------- Activity Life Cycle ---------
*/
@Override
public void onResume() {
super.onResume();
mApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
if (mApiClient.isConnected()) {
mApiClient.disconnect();
}
}
}
activity_rainstorm_map.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView">
<fragment
android:id="@+id/googleMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="ftag_google_map"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
代碼編譯正確,謝謝。它只是不顯示我的位置仍然或標記。我確實得到了位置按鈕,但它不會去我的位置。 – SeePlusPlus