Android: error with map fragment on navigation drawer and permission check - android

I started working on an old application i have that uses a map fragment on a navigation drawer. It used to work OK but now, with the new permisson thing, I get some errors. I have searched on SO and on the internet generally but I dont know what I am missing. My map Fragment is below
public class Fragment_maps extends android.support.v4.app.Fragment implements OnMapLongClickListener, OnMapClickListener, OnMapReadyCallback {
MapView mMapView;
GoogleMap googleMap;
#Override
public void onMapReady(GoogleMap googleMap) {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_maps, container, false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
//activate menu button
setHasOptionsMenu(true);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
//get the map
mMapView.getMapAsync(this);
// Detect location
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
//googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Turns traffic layer on
googleMap.setTrafficEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Turns on 3D buildings
googleMap.setBuildingsEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
// latitude and longitude
double latitude = 40.639350;
double longitude = 22.944607;
googleMap.setOnMapLongClickListener(this);
googleMap.setOnMapClickListener(this);
// create marker
MarkerOptions thessaloniki = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Thessaloniki");
// Changing marker icon
thessaloniki.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(thessaloniki);
CameraPosition cameraPositionThess = new CameraPosition.Builder() .target(new LatLng(40.639350, 22.944607)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionThess));
// Perform any camera updates here
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_maps, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected","yes");
switch (item.getItemId()) {
case R.id.HYBRID:
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
return true;
case R.id.SATELLITE:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return true;
case R.id.TERRAIN:
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
return true;
case R.id.NORMAL:
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onMapClick(LatLng point) {
Toast.makeText(getActivity(),point.toString(),Toast.LENGTH_SHORT).show();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
#Override
public void onMapLongClick(LatLng point) {
if (SelectUserActivity.flagOwner){
googleMap.addMarker(new MarkerOptions()
.position(point)
.title(MainActivity.onomaxarth)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Toast.makeText(getActivity(),"Hotel pinned at: "+point.toString(),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),point.toString(),Toast.LENGTH_SHORT).show();
}
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
I used googleMap = mMapView.getMap(); to get the map but i have change it with mMapView.getMapAsync(this); by implementing OnMapReadyCallback and it forced me to create the onMapReady method, which I dont know how it is different from the onCreateView method so i leave it empty.
Now i get an error on googleMap.setMyLocationEnabled(true); for permission and when i debug my app the googleMap object is null.
When I run it, on selecting the map fragment the app crashes and terminates the activity that holds the Fragments.
On the Manifest i have
android:minSdkVersion="17"
android:targetSdkVersion="23"
and my gradle dependencies are
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.android.gms:play-services:+'
What am i missing here? How can i use the permission in the fragment and why googleMap is empty?

Move this code from your onCreateView to your onMapReady:
// Detect location
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
//googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Turns traffic layer on
googleMap.setTrafficEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Turns on 3D buildings
googleMap.setBuildingsEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
// latitude and longitude
double latitude = 40.639350;
double longitude = 22.944607;
googleMap.setOnMapLongClickListener(this);
googleMap.setOnMapClickListener(this);
// create marker
MarkerOptions thessaloniki = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Thessaloniki");
// Changing marker icon
thessaloniki.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(thessaloniki);
CameraPosition cameraPositionThess = new CameraPosition.Builder() .target(new LatLng(40.639350, 22.944607)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionThess));
The line mMapView.getMapAsync(this); prepares the map and onMapReady gets called when the map is ready to be used.
Also, if you have defined private GoogleMap googleMap;, you will want to add this to the beginning of your onMapReady method:
this.googleMap = googleMap;

Related

Clustering User-Input Markers (Google Maps x Android Studio)

I am trying to cluster markers that users input onMapLongClick but I can't seem to figure it out. All the tutorials/samples I've found show how to cluster for objects that are at a predetermined LatLng. This won't work for what I want to do because the point is not known until the user places a marker. For example, if users input twenty markers while using the app in a certain location at zoom level 7, then I want to be able to cluster them based on distance or a grid or however when they zoom to level 6. I am using Google Maps with Android Studio. Can anyone please help? I've been working for awhile on this...Here is the code I have so far. Thank you!
public class BasicMapDemoActivity extends AppCompatActivity implements
OnMapReadyCallback {
GoogleMap mMap;
private UiSettings mUiSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.getItemId() == R.id.menu_legal) {
startActivity(new Intent(this, LegalInfoActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setScrollGesturesEnabled(true);
mUiSettings.setZoomGesturesEnabled(true);
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
mMap.addMarker(new MarkerOptions()
.position(point)
.snippet("this is a snippet"));
}
});
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
}
});
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
float maxZoom = 7.0f;
if (position.zoom > maxZoom) {
mMap.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
}
});
}
}
The easiest way is to attach a ClusterManager to your map. It will automatically animate the markers from cluster at their position (or cluster them, when you zoom out).
The clustering is part of another project, called android-map-utils which can be found here:
https://github.com/googlemaps/android-maps-utils
They have a great demo application in the repository, I believe you will find the functionality you need there.
Example for adding items to ClusterManager, after you map has been initialised.
set up the cluster manager
mClusterManager = new ClusterManager<>(BrowseLocationsActivity.this, googleMap);
mClusterManager.setClusterOnlyVisibleArea(true);
mClusterManager.setAlgorithm(new VisibleNonHierarchicalDistanceBasedAlgorithm<MapMarkerItem>(metrics.widthPixels, metrics.heightPixels));
when you need to add a new item to your cluster, just call
mClusterManager.addItem(new MapMarkerItem(latitude, longitude));
mClusterManager.cluster();
Note thatMapMarkerItem is a custom class, that implementes ClusterItem from the library.

Map does not detect touches/swipes in android

I followed this tutorial to add a map to one of my tabs. It works, but when I open the map tab I cannot swipe up/down, zoom in/out (except when I double click). Swiping left/right works, but the map moves very little. I made a customViewPager, and it disables swiping the tabs, but does nothing for he map.
At first I used tabActivity and my map fragment worked as it should, and looked like this:
mapActivity extends FragmentActivity...
Since tabActivity is deprecated I added sliding tabs, following the tutorial above. Map could not be added with FragmentActivity, only with Fragment so I changed map fragment to:
mapActivity extends Fragment...
Now I have the following problem - I cannot navigate the map. Swipe and touch inputs are not detected. I cannot move up or down, zoom in or out, and the only thing I can do is move from left to right, but it is very slow.
Try this. its sample . 100% working
public class Map extends Fragment {
final int RQS_GooglePlayServices = 1;
Location myLocation;
TextView tvLocInfo;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
// GPSTracker class
GPSTracker gps;
double latitude;
double longitude;
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_map_fragment, container, false);
// TODO Auto-generated method stub
MapsInitializer.initialize(getActivity());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) rootView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if (mapView != null) {
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 13);
map.animateCamera(cameraUpdate);
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
gps = new GPSTracker(getActivity());
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
// \n is for new line
// Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
// tvLocInfo = (TextView) findViewById(R.id.locinfo);
/* FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment) myFragmentManager.findFragmentById(R.id.map);
map = myMapFragment.getMap();*/
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
markerClicked = false;
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(11);
map.moveCamera(center);
map.animateCamera(zoom);
}
});
/* LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}*/
// Updates the location and zoom of the MapView
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
/* TextView tv = (TextView) getActivity().findViewById(R.id.textset);
tv.setText("Audio");*/
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
At first I added tabLayout and viewPager to appBarLayout in app_bar_main (I made an app with navigation drawer), and later I moved the appBarLayout to content_main and it worked.

Android: GoogleMaps on fragment LongPressClick doesn't work

I want to pin a new mark on my map but method onMapLongClick didn work.
My code is below. I have my map inside a fragment.
public class Fragment_maps extends android.support.v4.app.Fragment
implements OnMapLongClickListener, OnMapClickListener{
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_maps, container,
false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
//activate menu button
setHasOptionsMenu(true);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
//get the map
googleMap = mMapView.getMap();
// Detect location
googleMap.setMyLocationEnabled(true);
//googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Turns traffic layer on
googleMap.setTrafficEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Turns on 3D buildings
googleMap.setBuildingsEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
// latitude and longitude
double latitude = 40.639350;
double longitude = 22.944607;
// create marker
MarkerOptions thessaloniki = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Thessaloniki");
//----------------
MarkerOptions LeykosPyrgos = new MarkerOptions().position(new LatLng(40.626401, 22.948352)).title("Leykos Pyrgos");
// Changing marker icon
thessaloniki.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
//----------------
LeykosPyrgos.icon(BitmapDescriptorFactory .fromResource(R.drawable.whitetower));
// adding marker
googleMap.addMarker(thessaloniki);
CameraPosition cameraPositionThess = new CameraPosition.Builder() .target(new LatLng(40.639350, 22.944607)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionThess));
//----------------
googleMap.addMarker(LeykosPyrgos);
CameraPosition cameraPositionLeykosPyrgos = new CameraPosition.Builder() .target(new LatLng(40.626401, 22.948352)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionLeykosPyrgos));
// Perform any camera updates here
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_maps, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected","yes");
switch (item.getItemId()) {
case R.id.HYBRID:
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);;
return true;
case R.id.SATELLITE:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return true;
case R.id.TERRAIN:
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
return true;
case R.id.NORMAL:
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onMapClick(LatLng point) {
Toast.makeText(getActivity(),point.toString(),Toast.LENGTH_SHORT).show();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
#Override
public void onMapLongClick(LatLng point) {
googleMap.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
As you can see I have both onMapClick and onMapLongClick methods but when I click on map nothing happens. I also have implement OnMapLongClickListener and OnMapClickListener. What am I doing wrong?
Thanks in advance.
Your class implements the listener but you don't set it:
googleMap.setOnMapLongClickListener(this);
same with the normal click listener:
googleMap.setOnMapClickListener(this);

How to get lat n lng center point of map in android?

I am building an android application where user is getting latLng by long touching in Google map.
Here is the code:
public class MainActivitytut extends FragmentActivity{
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
private TextView myAddress;
String lat;
TextView txtmapinfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintut);
//onBackPressed();
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
// Setting a click event handler for the map
googleMap.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng arg0) {
// Getting the Latitude and Longitude of the touched location
latLng = arg0;
lat = latLng.toString();
// Toast.makeText(getApplicationContext(), lat,
// Toast.LENGTH_LONG).show();
// getMyLocationAddress();
// Clears the previously touched position
googleMap.clear();
// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Placing a marker on the touched position
googleMap.addMarker(markerOptions);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
}
});
}
protected void putExtra(String string, CharSequence text) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
// present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I had used lat = map.getCameraPosition().target; and when I toast the value of lat, it is toasting 0.0
What I need is that when on swiping the map the latlng change and toast.
Wait until the map has loaded and then call getCameraPosition:
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
LatLng pos = mMap.getCameraPosition().target;
Log.e("", "Pos: " + pos.toString());
mMap.addMarker(new MarkerOptions().position(pos));
}
});

Android Google Maps v2: on click listener not responding

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.
Instead of doing map = getMap() you can try this.
if (map == null) {
// Try to obtain the map from the SupportMapFragment.
this.map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapView))
.getMap();
setUpMap(lat, longi, R.drawable.marker2, title);
}
private void setUpMap(double lat, double lng, int drawableResource, String title) {
if(map != null) {
marker = map.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
.title(title)
.icon(BitmapDescriptorFactory.fromResource(drawableResource)));
geocoder = new Geocoder(this, Locale.getDefault());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15));
map.animateCamera(CameraUpdateFactory.zoomTo(13), 2500, null);
}
}

Categories

Resources