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) {
}
});
}
Related
I have a google map in my app and I handle clicks on it. Everything is fine but if I click on a marker or really close to it onMarkerClick() is called, not onMapClick() and then I don't have the exact location of the place I tapped because marker.getPosition() returns the center point of the marker what's not the same.
Is it possible to disable onMarkerClick() and have onMapClick() called even if I click on a marker?
Return true for disabling marker click event
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
For getting click on maps use this listener on onMapReady
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
#Override
public void onMapClick(LatLng arg0)
{
Log.v("onMapClick", "Yea worked!");
}
});
}
Complete code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
It is possible.Here is a simple solution.
First of all you should declare GoogleMap.OnMarkerClickListener and GoogleMap.OnMapClickListener in your activity or fragment like this.
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
And then register that interfaces in your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
Finally you can implement two interfaces.You should call onMapClick() method in onMarkerClick() method to achieve that even if you click on a marker like that.Don't forget to return true in onMarkerClick() method.
#Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
#Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
For very first time onMapLoaded() is not called. My map is not showing for first time. But onMapReady(GoogleMap googleMap) called. But at second time map is loaded succesfully. Both onMapReady(GoogleMap googleMap) and onMapLoaded() called. I can not predict the issue. Please let me any idea to resolve my issue.
For map initialize
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
For onMapReady:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(MapsActivity.this);
mMap.setMyLocationEnabled(true);
setCustomInfoWindow();
}
For onMapLoaded:
#Override
public void onMapLoaded() {
if (mMap != null) {
//mMap.snapshot(this);
addMarkers();
}
}
Add this mMap.setOnMapLoadedCallback(this); in onMapReady()
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapLoadedCallback(this);
mMap.setOnMarkerClickListener(MapsActivity.this);
mMap.setMyLocationEnabled(true);
setCustomInfoWindow();
}
Try it.
please don't confuse your self between onMapReady(GoogleMap googleMap) and onMapLoaded(). Here is a documentation you can refer
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.
Like title, my app crash if I add OnMarkerClickListener or OnInfoWindowClickListener. Please help me. I have updated the code
This is my code, sorry I'm newbie :|
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapsActivity.this);
btnBeHost = (Button)findViewById(R.id.btnBeHost);
requestQueue = Volley.newRequestQueue(this);
//be a host btn when clicked
...
//Welcome
...
//get coordinates of motels and show on map
//latitude request
...
//longitude request
...
RequestQueue queue = Volley.newRequestQueue(MapsActivity.this);
queue.add(latReq);
queue.add(lngReq);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this,"zzzz",Toast.LENGTH_LONG).show();
return false;
}
});
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
//set my location enabled
...
}
You are trying to call a method of mMap in your onCreate(), but mMap will be null until you assign it in onMapReady().
You will have to wait until after you assign it (perhaps later in onMap ready or in something you run subsequently) to call methods such as those which set up event listeners.
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))
);
}
});
}