我正在编写一个打开地图应用程序的应用程序,我希望能够在打开时在当前位置添加Marker
对象。我的应用程序正确打开地图应用程序,并显示我当前位置的蓝点,但我无法使用当前位置创建Marker
。将当前位置传递给标记
这里是我的源代码:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
public class PlaceMarker extends FragmentActivity
implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationClient mLocationClient = null;
private LocationRequest mLocationRequest = null;
private GoogleMap mMap;
private static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final int MILLISECONDS_PER_SECOND = 1000;
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;
private LocationManager locationManager;
private Location location = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_marker);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this, this, this);
if (servicesConnected()) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
Double mLatitude = getCurrentLocation().getLatitude();
Double mLongitude = getCurrentLocation().getLongitude();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(mLatitude, mLongitude))
.title("Title Test")
.snippet("Snippet Test"));
}
else {
Toast.makeText(this, "Position unavailable", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
protected void onStop() {
if (mLocationClient.isConnected()) {
mLocationClient.removeLocationUpdates(this);
}
mLocationClient.disconnect();
super.onStop();
}
private Location getCurrentLocation() {
Location location = mLocationClient.getLastLocation();
if (location != null) {
return location;
}
else {
Toast.makeText(this, "Current Location Unavailable", Toast.LENGTH_SHORT).show();
checkforGPSAndPromptOpen();
return null;
}
}
private void checkforGPSAndPromptOpen() {
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
// Handle results returned to the FragmentActivity by Google Play services
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST :
// If the result code is Activity.RESULT_OK, try to connect again
switch (resultCode) {
case Activity.RESULT_OK :
// Try the request again
break;
}
}
}
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("Location Updates", "Google Play services is available.");
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Get the error code
// Get the error dialog from Google Play services
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
errorDialog.show();
}
return false;
}
}
/*
* Called by Location Services when the request to connect the client finishes successfully.
* At this point, you can request the current location or start periodic updates
*/
@Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
mLocationClient.requestLocationUpdates(mLocationRequest, this);
location = getCurrentLocation();
takeToLocation(convertLocationtoLatLong(location));
}
/*
* Called by Location Services if the connection to the location client drops because of an error.
*/
@Override
public void onDisconnected() {
// Display the connection status
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
// String msg = "Updated location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude());
// Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
/*
* Called by Location Services if the attempt to Location Services fails.
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
private void takeToLocation(LatLng newLocation) {
if (newLocation != null) {
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(newLocation, 16);
mMap.animateCamera(update);
}
else {
Toast.makeText(this, "Position unavailable", Toast.LENGTH_SHORT).show();
}
}
private LatLng convertLocationtoLatLong(Location location) {
LatLng currentLatLong = new LatLng(location.getLatitude(), location.getLongitude());
return currentLatLong;
}
}
的logcat的错误表明,是由61行的IllegalStateException: Not connected
:
Double mLatitude = getCurrentLocation().getLatitude();
GPS在您的设备上已关闭也许?没有互联网连接可用? – Stan
@Stan GPS绝对开启,因为它不会显示我当前所在位置的蓝点。这真的让我很难过,因为我可以评论'mLatitude','mLongitude'和'addMarker()'线条,它会完美地打开地图,在我当前的位置显示一个蓝点。只要我重新注释这些行,就会发出抱怨没有连接的错误IllegalStateException。 – Jack
我相信,AOS缓存你的上一个位置,以便知道它,你不需要打开GPS系统。所以“GPS肯定会打开,因为它不会显示我当前位置的蓝点,如果它关闭的话”可能不那么清楚。此外,我使用定位服务管理当前位置,而不是从地图中提问。 – Stan