Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MapView display empty

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yu.lbs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name="com.yu.lbs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main.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" >

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey=".........."
        android:clickable="true"
        android:enabled="true" />

</LinearLayout>

MainActivity.java:

import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity {

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

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

The Activity can be started. But in the MapView there is nothing. The small grid of map can be show, I think that comes with MapView, but no map is loaded. What could be wrong? I am using Google API v3. But this code is from a text book using API v1.

like image 872
nomnom Avatar asked Sep 27 '13 05:09

nomnom


2 Answers

The way I got mine resolved, I had to do this:

final MapView mapView = (MapView)fragmentView.findViewById(R.id.map_fieldLocation);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
        googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
        mapView.onResume();
    }
});

The important part that I was missing is that you have to call the onCreate() method before you call getMapAsync() and once the callback is called, you need to call onResume() on the MapView object.

That totally solved it for me.

Here's what it would look like in your own class:

public class MainActivity extends MapActivity {

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

        if (getView() != null) {

            final MapView mapView = (MapView)getView().findViewById(R.id.mapView);

            mapView.onCreate(savedInstanceState);
            mapView.getMapAsync(new OnMapReadyCallback() {

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
                    googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
                    mapView.onResume();
                }
            }
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

Hope this helps!

like image 80
Simon Germain Avatar answered Sep 20 '22 21:09

Simon Germain


I guess you are making a mess from the version of Google Maps you are trying to implement. From the code that is used by you it's seems that you are trying to use Google Maps API V1.

The problem with that is that you can't produce now days a new API key for Google Map API V1, this version is deprecated and Google doesn't provide new keys for it.

from the comments it's looks that in the API Console you didn't activated the right API. Take a look at this blog post to get an Idea of how to produce an API Key for Google Map API V2 for Android:

Google Maps API V2 Key

Next, go over the following guide to implement this version in you application:

Google Maps API V2

like image 22
Emil Adz Avatar answered Sep 18 '22 21:09

Emil Adz