Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get map with Google Maps V2 MapView

I got Maps V2 working using FragmentActivity and SupportMapFragment but now I'm trying to get it working not using the above but with MapView. The Maps docs seem to say you can do it either way.

The app runs, but I just get a black screen, no map.

<?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="fill_parent"
    android:layout_height="fill_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

</LinearLayout>

The Java file:

package com.jps.mapstestthree;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.content.Context;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;


public class MainActivity extends Activity
implements OnMapReadyCallback
{

    private GoogleMap googleMap;
    private View view;
    private MapView mapView;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        context = (Context)getApplication();

        LayoutInflater inflater =(LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.activity_main, null);  
        mapView = (MapView)view.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try
        {
            initilizeMap();
        }
        catch (Exception e)
        {
        }

    }

    private void initilizeMap()
    {
        if ( googleMap == null )
        {
             mapView.getMapAsync(this);         
        }
    }

    @Override
    protected void onResume()
    {
        mapView.onResume();
        super.onResume();

        initilizeMap();
    }

    @Override
    public void onMapReady(GoogleMap map)
    {
        map.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Marker"));
    }
}

My AndroidManifest.xml is unchanged from when I did it using Fragments so I know that the Key, and everything else, is working.

And I removed the support v4 .jar since I didn't need it if Fragments where not used.

I tried having the other Methods, like onDestory(), defined, since it seemed to want all of them to do the MapView forwarding, but it complained about them, so I removed them.

And I tried doing more things in onMapReady(), but it didn't seem to do much good.

There are very few examples of using MapView for V2.

Is it possible to get a map showing without using Fragment and using MapView?

If so, what do I need to do, to get something showing up?

Update 1:

I tried several more things, but still no luck. I added back all the forwarded public final void onDestroy() etc. it didn't complain this time, and, in Logcat, it looked like it was still trying to reference Fragment in the support v4 .jar, so I even added back the support v4 .jar, re-did an android update project with --subprojects, and still I get a black screen, but it does have it's title.

Yes, It's the same project, I just renamed the main project name, the package name is the same, and the Key is the same as when it worked.

I also removed the top imports with maps.* and added the individual ones it needed, and it did a clean build.

UPDATE 2 :

I finally got it working without using the support v4 .jar! And any Fragments. I found the example source RawMapViewDemo.java in the play services samples, changed the app to use that basically, re ran 'android update project' with --subprojects, and it worked!!

Here is the full source, in the hopes that it might help someone.

package com.jps.mapstestthree;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;

public class MainActivity extends Activity
    implements OnMapReadyCallback
{

    // Google Map
    private GoogleMap googleMap;
    private View view;
    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView)findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        mapView.getMapAsync(this);
    }


    @Override
    protected void onResume()
    {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onMapReady(GoogleMap map)
    {
        map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
    }


    @Override
    public final void onDestroy()
    {
        mapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public final void onLowMemory()
    {
        mapView.onLowMemory();
        super.onLowMemory();
    }

    @Override
    public final void onPause()
    {
        mapView.onPause();
        super.onPause();
    }
} 
like image 503
user1572522 Avatar asked Jan 30 '15 00:01

user1572522


1 Answers

The update to your question leaves an air of mystery around how you solved this.

All that is required is to follow the API docs and forward (at least the specified) lifecycle callbacks of the parent Activity or Fragment to the MapView:

Users of this class must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

  • onCreate(Bundle)
  • onResume()
  • onPause()
  • onDestroy()
  • onSaveInstanceState()
  • onLowMemory()

And then, of course, use MapView#getMapAsync(OnMapReadyCallback) as specified in the API docs to obtain a reference to the GoogleMap used by the MapView.

like image 194
dm78 Avatar answered Sep 29 '22 17:09

dm78