I have a mapsview and it works fine when using getMap()
although it shows me a depricated warning. But when i change getMap()
to getMapAsync()
the app will crash.
Here's my code
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnInfoWindowCloseListener(this);
mMap.setOnMarkerDragListener(this);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
markerPoints.add(latLng);
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(latLng);
}
});
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//Getting the coordinates
toLatitude = marker.getPosition().latitude;
toLongitude = marker.getPosition().longitude;
dest = new LatLng(toLatitude, toLongitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(dest));
return ((toLatitude == my_marker.getPosition().latitude) && (toLongitude == my_marker.getPosition().longitude));
}
});
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
private void setUpMap() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
Toast.makeText(MapsActivity.this, "Anda harus menyetujuinya agar dapat menikmati semua fitur yang ada", Toast.LENGTH_LONG).show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
}
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setTrafficEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder()
.zoom(15) // Sets the zoom
.target(new LatLng(-6.597629,106.79957))
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Here's the xml layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This is the manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
What's wrong with my code? Any help will be greatly appreciated.
Thanks.