How to know if Google maps is ready to be used? - android

I have a simple activity with the follow segment code on it :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
tools:context=".MainActivity" />
</LinearLayout>
How to check in MainActivity.java if the map is fully loaded and ready ?

Before you can interact with a GoogleMap object, you will need to confirm that an object can be instantiated, and that the Google Play services components are correctly installed on the target device. You can verify that the GoogleMap is available by calling the
MapFragment.getMap()
or
MapView.getMap()
methods and checking that the returned object is not null.
An example of a test to confirm the availability of a GoogleMap is shown below. This method can be called from both the onCreate() and onResume() stages to ensure that the map is always available.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
References - https://developers.google.com/maps/documentation/android/map

Use GooglePlayServicesUtil.isGooglePlayServicesAvailable.

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.

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

Android GoogleMap V2 on zoom map doesn't get sharpen

I'm currently working on Android and have started with Google Maps V2 API.
As I have finished going through all their setup guide the map worked and I was able to see the continents. Only problem is that when I zoom in, the map stays the same I just get less pixels untill eventually I get gray squares - as if it has loaded the map but not entirely. I'd been looking around for someone with a problem like mine and couldn't find any.
Any assistance will be appreciated.
My code:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
public class myMapActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
More information:
I had added:
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
and gotten gray squares without map at all. hope it helps identify the problem Thank you in advance.
As recommended I had uninstalled which probably removed previous save of the map.
After reinstalling the app works fine.

GoogleMap class object throw Nullpointer Exception

friends, I am getting NullPointer error ,on addMarker()
Below is my sample code.
GoogleMap myMap;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
Error message like NullPointer at line
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context=".DemoMapActiviy" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
any idea how can i solve it?
I went through this issue a while back. First check to see if your fragment is found by the fragment manager.
If not try replacing the way you've put your fragment into your view. If you want an example of another way I've posted a maps example here
Any more questions feel free to comment and I'll work through it with you :)
EDIT
have you got the correct API key in there? If you can't contact Google's servers then there's a good chance that that's your problem. Otherwise may be a manifest issue make sure you have READ_GSERVICES and INTERNET permissions.
See Google Maps API V2 'Failed to Load Map. Could not contact Google Servers'
Edit 2
Ok so I was being lazy before and just referring you to my project. If you were getting a connection to maps in your project lets start from there.
What I was suggesting in my original response was to check and see if myMapFragment was null. If it is great try doing something along the lines of the following:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SupportMapFragment myMapFragment = new SupportMapFragment();
ft.replace(R.id.map, myMapFragment, Constant.TAXI_FRAGMENT_ID);
ft.commit();
Where R.id.map is an empty FrameLayout
If myMapFragment isn't null then try extending SupportMapFragment and running the following test:
public class MyMapFragment extends SupportMapFragment {
#Override
public void onActivityCreated()
{
if (getMap() == null) Log.e("TAG", "There's something very wrong here";
}
}
Also if you do this make sure that you are inflating an instance of MyMapFragment not of SupportMapFragment

The google map looks empty inside the simulator

public class Map extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.myMapView1);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
the map.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="#+id/myMapView1"
android:layout_width="fill_parent"
android:layout_x="0px"
android:enabled="true"
android:clickable="true"
android:apiKey="Obtained Key here" android:layout_y="105px" android:layout_height="fill_parent">
</com.google.android.maps.MapView>
</LinearLayout>
I check the log there's only one red mark, Failed to find provider info for com.google.settings. everything else seems to be fine. I am wondering why the map is empty looking?
The MapView is empty when your Maps API key is expired or not matching with your debug signature key.
Try to generate a new one.
Inside every MapActivity, the isRouteDisplayed() method is required,
so override this method:
#Override protected boolean isRouteDisplayed() {
return false; }
This method is required for some accounting from the Maps service to
see if you're currently displaying any route information. In this
case, you're not, so return false.
source: http://developer.android.com/resources/tutorials/views/hello-mapview.html
Additional the android:layout_x="0px looks a little bit dodgy to me. I think it's only used in AbsoluteLayouts so deleting it should not harm.
Please check logs whether you are not getting "unable to authenticate error from google server". If yes there is issue with map key.
Map key should be generate on same system where you are running application.
Also check whether you have google play account signed and location setting is enabled.
please check here for more details:
https://developers.google.com/maps/documentation/android/start

Categories

Resources