googlemap class in googlemapv2 - android

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

Related

GoogleMap complains of being a null object reference

I am working on an android application where I use maps and this is the error that I am getting
Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference
Prior to posting my query here I have looked for other similar issues but my code already have all what is being suggested so I was hoping if someone could point out the error.
This is the line which throws the error(second line).
I have no idea why it is caling map null when I've assigned it (at least I think I have)
private void initViews() {
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
//map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
and also this is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="65dp" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Use getMapAsync(...) instead of getMap().
Example can be found here.

Android SupportFragment returns Map Object as null

I am displaying a map in my android application like this:
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
The fragment is defined like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="28.635308"
map:cameraTargetLng="77.224960"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"/>
The problem is on some mobiles it is working fine while on others(android 2.2,2.3) it is not working. The map object is null in these cases.
To me it appears that the reason behind this is that google play services is not installed on these mobiles. Am i correct, if yes what is the possible workaround.

How to add a new google map view(the new with api v2 for android) as a item into a listview?

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() giving error

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

Google maps v2 on android devices with minSDK below 11

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();

Categories

Resources