Reputation: 27
I am using google map in my project. it was working but i did something( dont know what)now getMap() gives error. i think i update something. how can i fix it?
my code is like that;
GoogleMap googleMap;
..
if (locationManagerCheck.isLocationServiceAvailable()) {
if (googleMap == null) {
try {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();//ERROR
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_SHORT)
.show();
}...
i tried make it:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
but 'this' gives another error can you help me?
edit:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager_check locationManagerCheck = new LocationManager_check(
this);
if (locationManagerCheck.isLocationServiceAvailable()) {
if (googleMap == null) {
try {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(final Marker marker) {....}
}
@Override
public void onMapReady(GoogleMap Map) {
googleMap=Map;
}
}
Upvotes: 0
Views: 2627
Reputation: 1272
You should give a look to documentation
To add a map to an activity, add map fragment to your layout :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And then use it in your activity :
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(final Marker marker) {....}
}
The map will be created in async way, and the onMapReady function will be called when map is ready,in this function you will be able to add markers, change position, title, zoom or whatever.
Upvotes: 2
Reputation: 16191
At the point you're retrieving the map it is null
. You need to do it when you now the views have been initialised.
For example, in a Fragment
you could do it in onViewCreated
or in an Activity
in onCreate
after calling setContentView(R.layout.your_layout);
For example:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapView mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapView.getMapAsync(this);
}
Upvotes: 0