How to call getMapAsync() from an activity - android

I am trying to call getMapAsync() from An Activity but I got this error:
Error:(109, 14) error: cannot find symbol method getMapAsync(CategoryActivity)
public class CategoryActivity extends Activity implements View.OnClickListener,OnMapReadyCallback {
protected void onCreate(Bundle savedInstanceState) {
//requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapContainerLayout);
mapFragment.getMapAsync(this);
}

Try using an anonymous callback:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.a_main_maps);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: ");
}
});
Also, make sure you have the latest version of the play-services dependencies (9.2.0):
compile 'com.google.android.gms:play-services-maps:9.2.0'
Add Google Play Services to your project

Try defining a private Class implementing the OnMapReadyCallback Interface
and passing that object.

Related

getMapAysnc(this) crashes with NullPointerException

I've been trying to setup a map using some help I got here. SO, it's very simple code but crashes. What am I doing wrong?
public class ShowDirection extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap myMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_directions);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // causes the Exception
}
#Override
public void onMapReady(final GoogleMap map) {
this.myMap = map;
myMap.setMyLocationEnabled(true);
}
Double check if your Fragment's id is actually R.id.map because it's returning null on that line. If it is not, simply replace that with the proper id.

How to add a satellite view in android studio?

My map is working fine.However, i want to add a satellite view along with my normal view? How can i achieve that?
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng location = new LatLng(x,y);
mMap.addMarker(new MarkerOptions().position(ReduitBusStop).title("you are here!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
Try with setting the type of map tiles as below
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Use the below function to set the satellite view
setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE);

android SupportMapFragment : java.lang.NullPointerException

I am getting a java.lang.NullPointerException on the line map.setOnMyLocationChangeListener(myLocationChangeListener);. Strangely, this exception arises while running it some devices while in some it doesn't. How do I solve this?
public class MyMap extends ActionBarActivity implements
RoutingListener, OnMapReadyCallback,
SensorEventListener {
// private GoogleApiClient mGoogleApiClient;
protected GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
}
EDIT:
#Override
public void onMapReady(GoogleMap mMap) {
map = mMap;
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setCompassEnabled(true);
updateMyLocation();
}
By calling mapFragment.getMapAsync(this);, I suppose you have to override this method public void onMapReady(GoogleMap googleMap). Set your mMap = googleMap there and set all other listener there. Keep in mind that your Map object needs some time to be initialised, that's why they give you getMapAsync method.
Editted:
Please remove this from your onCreated
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
and put them map.setOnMyLocationChangeListener(myLocationChangeListener); inside your onMapReady() after this line map = mMap. Please acknowledge when your map is initialized.
When you work with GoogleMap then it takes time to initialize. Sometimes it takes longer time in some devices than others.
To solve this issue, you should initialize the GoogleMap object with some delay. You can follow the below strategy:
private Handler handler = new Handler();
private Runnable mapChecker = new Runnable() {
#Override
public void run() {
try {
if (mapFragment.getMap() != null) {
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
handler.postDelayed(mapChecker, 500);
}
};
Then from onCreate(),
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
..........
handler.postDelayed(mapChecker, 500);
..........
}

Replace getMap with getMapAsync

I want to replace the deprecated getMap Method with getMapAsync, but I didn't use MapFragment but GoogleMap like this:
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
try {
if(googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
If I replace the googleMap with MapFragment like this I'm not able anymore to setMapType and so on. So how can I change to getMapAsync in my case?
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
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);
}
}
Very simple, just have your Activity implement the OnMapReadyCallback interface, and then assign your googleMap reference in the onMapReady() callback.
Then, perform any actions on googleMap that you want.
Here is a simple example:
public class MainActivity extends Activity implements OnMapReadyCallback {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap(){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
}
If you use MapFragment you can still use stMapType method. Implement OnMapReadyCallback on your activity.
Create MapFragment and call getAsyncMap method on that object.
It will ask you to inplement onMapReady(GoogleMap map) method there you can set map type and so on.
Hope it helps.
The better way I found to understand the use of getMapAsync was to create a new project in Android Studio and use the template of google maps activity instead of an empty activity.
You will have a project that works to study and adapt your owns.

How to set up OnMapLoadedCallback GoogleMaps

I want to know when everything I have being loaded into my googlemap fragment has loaded everything. This is what my code currently looks like
public class MainMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLoadedCallback {
GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//addLatestLocations();
// TestVectorQuery();
}
#Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(32.0516, -78.925), 3));
map.setOnMapLoadedCallback(this);
}
#Override
public void onMapLoaded() {
//SetMapMarker(26.1333, 80.1500);
//Whatever I want to do
}
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
There doesn't seem to be anything wrong with this; however, when I try to do something within onMapLoaded() nothing happens, so it seems that the callback isn't properly set. Any help would be appreciated. Thank you.
You are using a native fragment (com.google.android.gms.maps.MapFragment) and getFragmentManager() in a FragmentActivity. That does not work. Either change the fragment to SupportMapFragment and use getSupportFragmentManager(), or change your activity to inherit from Activity.
try to use support fragment because it works for me .
below is my code run successfully.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_map);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
map.addMarker(new MarkerOptions()
.title("your title")
.snippet("your desc")
.position(new LatLng(-22.2323,-2.32323))
);
}
});
}

Categories

Resources