I'm trying to put a marker when the user clicks on map. I'm using a SupportMapFragment
inside an ActionBarActivity
. But the map doesn't respond, furthermore, a map.setMapType()
operation isn't working.
Here's my code:
private GoogleMap map;
private Marker marker;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.getSupportActionBar().setTitle(R.string.select_location);
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.map, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d("Map","On view created");
map = getMap();
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
Log.d("Map","Map clicked");
marker.remove();
drawMarker(point);
}
});
...
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
//PLACE THE INITIAL MARKER
drawMarker(new LatLng(location.getLatitude(),location.getLongitude()));
}
}
Logcat is showing the "on view created" message and the map shows the current location with a marker, so the last part of the code is being executed. But the onMapClickListener
is overrided or something because it doesn't work, and the map isn't a satellite.
Can anybody help me?
If you have extended SupportMapFragment you can simply do this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.getSupportActionBar().setTitle(R.string.select_location);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = getMap();
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
Log.d("Map","Map clicked");
marker.remove();
drawMarker(point);
}
});
Note that getMap() is called in onActivityCreated
and inflater.inflate(R.layout.map, container, false)
is not necessary if you don´t use a custom layout.
You don´t even need a map.xml layout!
You are extending SupportMapFragment
but you are inflating another MapView
(not the one tied to SupportMapFragment
by default), that´s why you are not viewing the changes in your map. Because you were acting on the default View
got from getMap()
but you are viewing another. See the docs about getMap():
public final GoogleMap getMap ()
Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.
Hope it helps ;)
Well you are placing a listener for your map, but you need to make a listener for your markers.
map.setOnMarkerClickListener(this);
...
@Override
public boolean onMarkerClick(Marker arg0) {
Log.i(TAG,"marker arg0 = "+arg0);
return false;
}
or for the InfoWindows on top of the markers:
map.setOnInfoWindowClickListener(this);
Also, your initialitizing of map making it show satelite is almost correct:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
change it to:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Instead of doing map = getMap(), a few lines later I had this code:
SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
So I've just put the code above that line and it's done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With