I have added a map fragment to my application and added the following code to place a marker on the fragment.But I'm getting errors in the code snippet.I have included all relevant imports.
The errors are compile time and are as follows:
1.MapFragment cannot be resolved to a type.
2.GoogleMap cannot be resolved to a type.
3.LatLng cannot be resolved to a type.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
Anyone have any ideas where the error is in the snippet? Thanks
I think you should check two things first
Is the Google Play Service project or library added to your project ? If not, check this link http://developer.android.com/google/play-services/setup.html
If you compile for android version < 11, you need to use supportFragmentManager instead of the normal FragmentManager :
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
In the last case, you also need to update the class used in your XML to class="com.google.android.gms.maps.SupportMapFragment"
Related
i got this error when i extends ItemizedOverlay on Activity. How can i resolve this error ? can any one help me out here.
ItemizedOverlay is no longer available in Google Maps v2.
If you would like to display marker on a map you can user Marker class. You can create Marker object using MarkerOptions.
I'm attaching the simplest example:
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(52.2330653, 20.9211106))
.title("Warsaw"));
}
In addition I recommend reading Google Maps Android API and Markers section.
You can make a marker draggable using:
MarkerOptions.draggable(boolean)
Marker.setDraggable(boolean)
You can also attach marker drag listener, you can find more here.
In my Android application my map screen shows direction and Google map when click marker on the map. I use the following in my application.
XML:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In code:
GoogleMap googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
I have marked the direction and Google map icon marked in blue color. please see the image of my map screen.
How to hide direction and Google map icon from map fragment?
Google Map provides a simple boolean method for this:
gmap.getUiSettings().setMapToolbarEnabled(false);
And You are done.
TL;DR version:
Try overriding OnMarkerClickListener and return true.
Longer version:
When you return true you say to GoogleMap
I, the developer, handled a click on the Marker. You, the GoogleMap, don't have to do anything.
So as a result GoogleMap does not perform its default action which in this case would be: show those buttons (which you don't want).
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// for Map tool disable
mMap.getUiSettings().setMapToolbarEnabled(false);
// for Zoom Button Enable on Google Map
mMap.getUiSettings().setZoomControlsEnabled(true);
//for Location Button enable on Google Map
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
It's Called Map toolbar
You can enable and disable the toolbar by calling UiSettings.setMapToolbarEnabled(boolean).
I started a new project with the Google Maps template of Android Studio and just added a marker to the map.
LatLng location = new LatLng(lat, lng);
Marker marker = mMap.addMarker(new MarkerOptions()
.position(location)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
When I click the Marker there comes a little menu with a Google Maps icon on the bottom right corner:
How can I get rid of this thing? I couldn't find anything I even don't know how to call it but I really need to get rid of it.
You need to call setMapToolbarEnabled method of UiSettings
Like this:
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMapToolbarEnabled(false);
// Do other staff
}
I'm making a Navigation based application for Android using API. I have successfully added Google Maps to my app. But now I want to add navigation marker (arrow like symbol) same as we see in official Navigation app of Google. I searched allot but didnt found anything. So can anyone help me out?
There is a link to a good read about this.
Try this:
GoogleMap map;
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
I have the following activity in my app that sets a geo location on a map fragment but when I click the activity in my testing it crashes with the following errors:
http://pastebin.com/6QhdL7z9
I looked through the errors but I can't make sense of why it is crashing as its referring to main as one of the reasons.
This is where I'm calling the code in the onCreate() for reference:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_gmit);
/*map = (MapView)findViewById(R.id.map);
map.setBuiltInZoomControls(true);*/
//code to add a map marker at GMIT geographic location
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(53.271900177, -9.04889965057))
.title("GMIT Galway Location"));;
}
Android Studio or Eclipse?
Did you try searching on SO? ;)
com.google.android.maps.MapView ClassNotFoundException
Android - Google Maps API v2 - NoClassDefFoundError
Sounds like you did not export the library in your project.....