Ich habe bereits die zuvor genannten Lösungen ausprobiert.Google Maps - null Zeiger Ausnahme
public class LocationActivity extends FragmentActivity implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {
private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
GoogleMap googleMap;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
finish();
}
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setContentView(R.layout.activity_location_google_map);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
fm.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
private void addMarker() {
MarkerOptions options = new MarkerOptions();
IconGenerator iconFactory = new IconGenerator(this);
iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime)));
options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
options.position(currentLatLng);
Marker mapMarker = googleMap.addMarker(options);
long atTime = mCurrentLocation.getTime();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
mapMarker.setTitle(mLastUpdateTime);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
13));
}
}
}
Ich versuche, eine Markierung mit dem Label als Zeit haben die Breite und Länge, bei dem hinzufügen, um die GPS gemessen wurden. Aber wenn diese Methode aufgerufen wird, erhalte ich einen Fehler: addmarker (..) wurde auf Null-Objekt aufgerufen. Der Längen- und Breitengrad wurde im Debug-Modus korrekt angezeigt. Also, anscheinend ist die Karte selbst null. Könnte mir jemand sagen, wie ich mit diesem Problem fortfahren soll? Ich habe den entsprechenden Code angegeben. Vielen Dank im Voraus :)
Check hier aus: http://stackoverflow.com/questions/38388282/finding-current-location-of- the-user-in-android/38397092 # 38397092 –
Es ist nicht klar aus Ihrem Code, wo und wann Sie Ihre addMarker-Funktion aufrufen. Ist es nach dem Aufruf von onMapReady? –
@Override public void onLocationChanged (Standort) { mCurrentLocation = Standort; mlaustUpdateTime = DateFormat.getTimeInstance(). Format (neues Date()); addmarker(); } – Jab