How to set up OnMapLoadedCallback GoogleMaps - android

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))
);
}
});
}

Related

Google Maps looks like an image

I have two Google Maps in my application, which I have implemented in the same way. One is working and one is not. The latter looks like an image and I can't interact with it. I can add markers and move the camera, but the info window doesn't show when I click the marker I can't move it, zoom in and zoom out. It looks like an image, a preview.
Here is the code:
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
private static Double latitude;
private static Double longitude;
private static String title;
public static MapFragment newInstance(Double mLatitude, Double mLongitude, String mTitle) {
latitude=mLatitude;
longitude=mLongitude;
title=mTitle;
MapFragment fragment = new MapFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map_details);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setAllGesturesEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
.position(new LatLng(latitude, longitude))
.title(title));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(12f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.moveCamera(cameraUpdate);
}
public interface OnMapFragmentInteractionListener {
void onMapFragmentInteraction(Uri uri);
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
}
}
You should take a look at XML. There is liteMode and when it's set to true it does exactly what you are describing.
<com.google.android.gms.maps.MapView
android:id="#+id/row_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:liteMode="true"
map:mapType="none" />
Here look at documentation
I'm just guessing but in code that you posted i see one mistake. To be sure you have to check your imports in the place when you try to use your MapFragment. As I said I guess that in one place it's imported from google pacage and another one from your class. The problem is that the MapFragment that you post here isn't a google MapFragment because you extants it from simple Fragment. To be sure rename your own MapFragment to CustomMapFragment and try to extands from MapFragment or just use it without customize.

how to identify which fragment is clicked in onMapReady method

i have an activity which have two fragment attach to it.Both of them show google map.this is the xml output
now i want to achieve is that i want to know which fragment is called in side onMapReady method
here i am setting the 2 fragments
gmap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map));
gmap2 = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2));
here i am setting getMapAsync for both of them
gmap.getMapAsync(this);
gmap2.getMapAsync(this);
now here i want to know which fragment map is clicked
#Override
public void onMapReady(GoogleMap googleMap) {
//here i want to know which fragment is clicked
}
You have to add 2 different Listeners like this:
gmap.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
}
});
}
gmap2.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
}
});
}

Marker is not working(not exact location is displaying), only world Map is visible in Frgament

CustomMapFragment
public class CustomMapFragment extends SupportMapFragment {
public static final LatLng NurissLife = new LatLng(28.602012, 77.098750);
private SupportMapFragment supportMapFragment;
#Override
public void onActivityCreated(Bundle bundle) {
// FragmentManager fm=getChildFragmentManager();
supportMapFragment=SupportMapFragment.newInstance();
//supportMapFragment=(SupportMapFragment)getChildFragmentManager
//().findFragmentById(R.id.map_container);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
super.onActivityCreated(bundle);
}
}
Fragment xml
<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"
android:id="#+id/map_content"
tools:context="com.demo.stuartabhi.nurisslife.Fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:layout_width="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:id="#+id/map_container">
</FrameLayout>
</FrameLayout>'
Marker is not working(exact location is not displaying), only world Map
is visible in Fragement
OnMapReady is not working either showing
no error in log, location code being described in OnMapReady method.
Please help regarding this, how to access the described location LatLng NurissLife inside SupportMapFragment.
Thanks in advance.
If you extend SupportMapFragment, there is no need to inflate any xml layout. Just call this.getMapAsync() in order to get a reference to the GoogleMap:
public class CustomMapFragment extends SupportMapFragment implements
OnMapReadyCallback {
private GoogleMap mMap;
public CustomMapFragment() {
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("MyMap", "onMapReady");
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(NurissLife).title("Nuriss LifeCare"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NurissLife, 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
}
Try this:
Create a method getCustomMapFragment() in your Activity class like this:
private SupportMapFragment getCustomMapFragment(){
SupportMapFragment supportMapFragment;
supportMapFragment= SupportMapFragment.newInstance();
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
return supportMapFragment;
}
And when you perform your FragmentTransaction, try this:
...
transaction=getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_container,getCustomMapFragment()).commit();
...
Also, with this implementation, the class="com.google.android.gms.maps.SupportMapFragment" line in your XML might be unnecessary.

android google maponClick not responsive

I have a map in my layout which when clicked should addmarker to it and display it on map.But the onMapClick method is not triggered when clicked.Below is the code":
Details.java:
public class Details extends AppCompatActivity implements OnMapReadyCallback,GoogleMap.OnMapClickListener {
SupportMapFragment mapFragment;
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map=googleMap; LatLng bolg = new LatLng(9.984616,76.267082);
map.addMarker(newMarkerOptions().position(bolg).title("Demo")).showInfoWindow(); map.moveCamera(CameraUpdateFactory.newLatLng(bolg));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
#Override
public void onMapClick(LatLng latLng) {
Log.e("LongClick","clicked");
map.addMarker(new MarkerOptions().position(latLng).title("X")).showInfoWindow();
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
details.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/map1"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
To detect user click event on GoogleMap, modify the AppCompatActivity
implements OnMapClickListener, override onMapClick(LatLng point), and
call map.setOnMapClickListener(this).
map.setOnMapClickListener(this);
In your onMapReady set an on click listener map.setOnclicklistener
After getting google map from mapfragment you need to do the following for receiving click events.
GoogleMap mGoogleMap = mapFragment.getMap();
mGoogleMap.setOnMapClickListener(this);
you need to implement this listener also GoogleMap.OnMapClickListener

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.

Categories

Resources