Ich bin ein Anfänger in Android-Programmierung und arbeite derzeit an Google Maps. Was ich tun möchte ist, den aktuellen Standort des Benutzers zu erhalten und dann einen Marker an diesem Ort zu setzen.Markierung am aktuellen Standort hinzufügen
Ich übe darauf, aber ich kann es wirklich nicht zur Arbeit bringen. Dies zeigt nur den aktuellen Standort des Benutzers. Aber ich möchte auch einen Marker darauf zeigen. Irgendwelche Vorschläge wie es geht?
public class MapsActivity extends AppCompatActivity implements GoogleMap.OnMyLocationButtonClickListener,
OnMapReadyCallback,
ActivityCompat.OnRequestPermissionsResultCallback {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private boolean mPermissionDenied = false;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener(this);
enableMyLocation();
}
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
android.Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "Showing current location", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return;
}
if (PermissionUtils.isPermissionGranted(permissions, grantResults,
Manifest.permission.ACCESS_FINE_LOCATION)) {
enableMyLocation();
} else {
mPermissionDenied = true;
}
}
Im Dokumentationsbereich: [Karte Aktuellen Standort in einer Google Map] (http: // Stackoverflow. com/documentation/android/170/google-maps-api-v2-fuer-android/5557/show-aktueller-standort-in-einem-google-karte) –
@MarkusKauppinen Danke! Habe es jetzt funktioniert. – Gelly