Reputation: 525
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:
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.
Upvotes: 2
Views: 6286
Reputation: 3806
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()
}
}
Upvotes: 1
Reputation: 148
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.)
Upvotes: 6
Reputation: 3304
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.
Upvotes: 1
Reputation: 115
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
Upvotes: -3