Replace getMap with getMapAsync
as in the official doc, get map async requires a callback;
it's there your "main entry point" for google maps stuff!
public class MapPane extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
// DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
changing getMap to GetMapAsync
UPDATE
Define private GoogleMap gMap
and implement OnMapReadyCallback
in your main Activity and implement onMapReady
method
in onMapReady
you should write this linegMap = googleMap
, then you can do what you want in onMapReady
- getting current location
- custom setting for your map
- and ...
now it's time to use this lines in your OnCreate
method
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
it should solve your problem, hope it helped you
How to use getMapAsync() instead of getMap()
your class need to implements OnMapReadyCallback
public class FindParty extends AppCompatActivity implements OnMapReadyCallback {
static final LatLng Durban = new LatLng(-29.858680, 31.021840);
private GoogleMap googleMap;
MapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_party);
try {
if (googleMap == null) {
mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
final Marker marker_Durban = googleMap.addMarker(new MarkerOptions()
.position(Durban)
.snippet("Durban, KwaZulu-Natal, South Africa")
.title("Durban"));
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(FindParty.this, Party.class);
intent.putExtra("message", "Durban");
startActivity(intent);
}
}
});
}
}
Replace getMap with getMapAsync with custom support fragment
You can't get GoogleMap
object by calling
mMap = mMapFragment.getMapAsync();
you should use code like that:
mMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
});
where mMap
is global variable:
public class MainActivity extends AppCompatActivity {
private GoogleMap mMap;
...
and after you got mMap
you can use it for setup your map:
mMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (mMap != null)
setUpMap(needUpdate);
}
});
Also, take a look at Official Tutorial.
cannot resolve getMap() or replace with getMapAsync()
The getMap()
method was deprecated and then removed, so you'll need to use getMapAsync()
instead.
There is no need to override onCreateView()
when the Fragment extends SupportMapFragment directly.
Instead, just call getMapAsync()
from the onResume()
override, and use the Google Map reference that is returned in the onMapReady()
override:
public class RunMapFragment extends SupportMapFragment {
private static final String ARG_RUN_ID = "RUN_ID";
private GoogleMap mGoogleMap;
public static RunMapFragment newInstance(long runId) {
Bundle args = new Bundle();
args.putLong(ARG_RUN_ID, runId);
RunMapFragment rf = new RunMapFragment();
rf.setArguments(args);
return rf;
}
@Override
public void onResume() {
super.onResume();
if (mGoogleMap == null) {
getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);
}
}
Note that if you are targeting api-23 or higher, you'll need to ensure that the user has approved the location permission at runtime before using the setMyLocationEnabled()
method, for more info see my answer here.
Android getMapAsync in Fragment
You are initializing map without even inflating the view....so your mapFrag
is null.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initializeMap();
return inflater.inflate(R.layout.need_help_view, container, false);
}
Try this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.need_help_view, container, false);
if (gm == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView);
mapFrag.getMapAsync(this);
}
return v;
}
Google Maps .getMap() deprecated
You should replace getMap() with getMapAsync().
As per the official doc, get map async requires a callback and this is where yo do main google maps stuff. Have a look on the below snippet.
public class MapPane extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
Hope this helps you.
Related Topics
How to Add a Button to Preferencescreen
How to Properly Highlight Selected Item on Recyclerview
How to Get the Current Location Latitude and Longitude in Android
Android: Keeping a Background Service Alive (Preventing Process Death)
Android Studio Does Not Show Layout Preview
Where Is Android.Os.Systemproperties
Where Is the Layout Preview in Android Studio
Android Facebook Sdk 4 in Eclipse
Android Sample Bluetooth Code to Send a Simple String via Bluetooth
Error Android Emulator Gets Killed in Android Studio
Best Method to Download Image from Url in Android
Android 6.0 Marshmallow. Cannot Write to Sd Card
Error Opening Supportmapfragment for Second Time
Android Studio 0.4 Duplicate Files Copied in APK Meta-Inf/License.Txt
How to Build an Executable for Android Shell
Android - Storing/Retrieving Strings with Shared Preferences