i have problem with this line when I create project which use Google maps API v2.
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
It's says that I need to set minSDK to be minimum 11, but I want to use this application also on devices with android minSDK 8 (2.3.3). Any ideas? Or simply: How I can set google maps API v2 to be compatible with devices with minSDK 8.
Thanks
Use SupportMapFragment from a FragmentActivity, instead of MapFragment from an Activity. To use fragments on devices older than API Level 11, you need to use the Android Support package's backport of fragments (where FragmentActivity comes from).
http://android-er.blogspot.de/2012/12/using-supportmapfragment.html - small example
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
AND:
package de.meinprospekt.android.ui;
import java.text.SimpleDateFormat;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment fragment = getSupportFragmentManager().findFragmentById(
R.id.map);
SupportMapFragment mapFragment = (SupportMapFragment) fragment;
GoogleMap mMap = mapFragment.getMap();
Related
I am working on an AVD with the specs of a Galaxy S6. There I can use the SupportMapFragment without problems.
But when a customer tries to use the App including the Map on a Galaxy S3 Device with 4.9" and Android Version 4.03, the App doesn't show the Map.
What can I do?
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ibas.locatixteamviewer.MapsActivity">
<!--<Button-->
<!--android:id="#+id/button1"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:background="#359c5e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true"-->
<!--android:padding="8dp"-->
<!--android:layout_margin="5dp"-->
<!--android:src="#drawable/menu_car_side_7_white"-->
<!--android:textColor="#ffffff" />-->
</fragment>
//----------------------------
#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);
Thanks!
I am using SupportMapFragment to display Google Maps in my app:
SupportMapFragment mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mMapFragment == null) {
mMapFragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_frame, mMapFragment, MAP_FRAGMENT_TAG).commit();
}
mMapFragment.getMapAsync(this);
This is the xml part:
<FrameLayout
android:id="#+id/map_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/button_status_view">
</FrameLayout>
Unfortunately, the map doesn't show the Google logo which causes violation on the Google Maps APIs terms of service (https://developers.google.com/maps/terms) In particular clause 9.4(a), which does not allow developers to delete or alter the Google logo on the map displayed in your app.
How can I add the Google logo to the map? Should it be there by default? Am I missing something? The fragment currently shows only the GPS button.
use MapFragment instead of Frame Layout:
<fragment android:id="#+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.googlemapdemo.MapsActivity"/>
i am currently working with googlemap v2. the problem is that i can't use the class "googlemap" it shows an error. i am using google api 4.2. how can i use this class. xml file code is given below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
If you use
class="com.google.android.gms.maps.SupportMapFragment"
Then you have to use the SupportMapFragment class in your activities.
Example:
SupportMapFragment mapa;
mapa = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
Hope it helps
You need to reference GooglePlay services in your android project and import
import com.google.android.gms.maps.GoogleMap;
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap
You can follow
http://developer.android.com/google/play-services/setup.html
and Reference
Importing google-play-service library showing a red X next to this reference android
I am using a mapview in listview,it's a part of item view in the listview,now i need to change the api v1 to v2,but i can not,because the new api only support the GoogleMap view with a frgment,i don't know how to implements that,it always say "java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment".It's anyone here can help?Thank you very much!
the key code what i use is same with blew:
in layout,i add the follow code to a LinearLayout:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and in the java code,i try to get the GoogleMap view in this way(in the getView method of my own adapter with extends the BaseAdapter),here is the code:
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
The error you recieve derived from the fact that you are trying to get a MapFragment object as it's specified in your XML file using the SupportFragmentManager object.
Instead use the FragmentMananger object to get the MapFragment, like so:
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Of course this depends on what is you target API you develop you application to. if your taget api > 11 then use the following line and a simple Activity class. in other case change you XML object of the map to SupportMapFragment and use the line you have used:
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
in this case you Acitivity should extend an FragmentActivity object.
try using like this
`
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
`
and java code like this
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
and make sure your class extends FragmentActivity
setContentView() is giving error in below is my code :
public class LocateUserInMap extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locate_user_in_map);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(19.111693900000000000,72.911271500000000000);
mMap.addMarker(new MarkerOptions().position(CIU).title("Friend"));
// findDirections(19.111693900000000000,72.911271500000000000,19.128203900000000000,72.928065000000060000, GMapV2Direction.MODE_DRIVING);
}
}
error :
here is my library that I am using :
and activity_locate_user_in_map.xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LocateUserInMap" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
what I am missing...
Please help!!!
Regards,
Sourabh
Your layout file R.layout.activity_locate_user_in_map is missing, maybe it is on another project or it is in correct project but somehow it can't be resolved, try to clean the project
The #SuppressLint("NewApi") really shouldnt be needed for onCreate() so I am going to guess that you have set your project to the something like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
And then are trying to use Fragments without using the support library.
You will need to change the imports to something like:
import android.support.v4.app.FragmentActivity;
And change any calls to:
getFragmentManager()
to
getSupportFragmentManager()
You might also like to checkout this question for a good background on Fragment, FragmentActivity and the support lib