2

first off all I know here are many threads about this problem, but I read them all ,and try pretty much everything. So what is my problem. I am developing an app with Google maps, and I also occur that well known problem that mapView is loaded fine, but it contains nothing (only grey blank rectangles).

Here is what I tried:

  • I tripplecheck my API code
  • I regenerate my API code
  • I check all the permmisions
  • And a lot of other stuff

main_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:enabled="true"
    android:apiKey="my key"/>

</RelativeLayout>

manifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gps.gpsclientsoftware"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



    <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.gps.gpsclientsoftware.GPSClientActivity"
            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>

activity code:

package com.gps.gpsclientsoftware;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

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

public class GPSClientActivity extends MapActivity {

    private MapView mapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);


    }


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

}

One warning that I found when I launch my app, was:

03-11 17:51:03.751: E/MapActivity(8581): Couldn't get connection factory client

Hope you can help me.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ján Srniček
  • 505
  • 1
  • 10
  • 34

4 Answers4

6

Check this...

http://ucla.jamesyxu.com/?p=287

You need to override all the method. Don't miss any one.

I did it, and I got a working MapView.

(onLowMemory seems can be skipped.)

(MapView works better than MapFragment.)

hsu.tw
  • 148
  • 2
  • 10
1

Since 12/2012 Google released Google maps version 2. This means that new applications should use this version and that api keys are provided only for v2 maps.
Your implementation seems to be for google maps v1.
Check here for a detailed guide, but from a quick look I see that the following are missing from your android manifest:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_api_key"/>
<permission
        android:name="your.project.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

<uses-permission android:name="your.project.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your key" />

Also to run google maps on your phone you need to install google play services. In order to check that it is already installed and google maps v2 work on your device I suggested using an application that uses v2 maps like trulia.

Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
1

As hsu.tw pointed out, we need to call lifecycle method of MapView from fragment lifecycle methods.

But having MapView part of layout file causes issue as onCreate called before inflating fragments layout [So MapView's onCreate() never be called].

Below approach works for me. [Might need to take care of removing MapView if you are adding this Fragment to BackStack]

class MapFragment : BaseFragment() {

    var mapView: MapView? = null

    override fun getFragmentLayout() = R.layout.fragment_map

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        (view as? LinearLayout)?.addView(mapView, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // create map view
        mapView = MapView(context /*, GoogleMapOption for configuration*/)
        mapView?.onCreate(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        mapView?.onStart()
    }

    override fun onPause() {
        super.onPause()
        mapView?.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView?.onResume()
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        mapView?.onSaveInstanceState(outState)
    }

    override fun onStop() {
        super.onStop()
        mapView?.onStop()
    }

    override fun onLowMemory() {
        super.onLowMemory()
        mapView?.onLowMemory()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDestroy()
    }
}
Suryavel TR
  • 3,576
  • 1
  • 22
  • 25
-3

Replace "my key" in your manifest with your actual API key.

android:apiKey="my key"/>

An API key can be otained here: https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key

usb79
  • 115
  • 3
  • i replace my current key with words my key just here :) i have properly obtained my api key from google – Ján Srniček Mar 11 '13 at 17:19
  • oh, ok. sorry then :) did the same API key work before? because some guy posted that ""Couldn't get connection factory client" is a weird message for an invalid apiKey !": http://stackoverflow.com/questions/2199403/couldnt-get-connection-factory-client probably try obtaining a fresh key, if it did not work before yet... – usb79 Mar 11 '13 at 17:27