谷歌地图图块仅在采取行动后加载

时间:2014-04-07 12:33:23

标签: android google-maps google-maps-api-2

启动我的应用时遇到一个小问题。应该显示的瓷砖只有在点击地图后才会加载......之后一切似乎都能正常工作!

在logcat中,我看到以下错误:

  

E / EnterpriseContainerManager:ContainerPolicy服务还没有   准备!!!

public class MainActivity extends FragmentActivity {
  private static final String TAG = "MAIN_ACTIVITY";
  private MyLocationListener locListener;
  private SupportMapFragment supportMapFragment;
  private GoogleMap gmap;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setUpMap();
    list = new ArrayList<GPSCoordinates>();

    new Object();
  }

  @Override
  protected void onStart() {
    super.onStart();
    if (gmap == null) {
        gmap = supportMapFragment.getMap();
        Log.i(TAG, "gmap set!");
    }
    if(locListener == null) {
        this.initLocationManager();
    }
    Log.i(TAG, "gmap start!");
  }

  @Override
  protected void onStop() {
    super.onStop();
    Log.i(TAG, "gmap stop!");
  }

  private void setUpMap() {
    GoogleMapOptions options = new GoogleMapOptions();
    options.mapType(GoogleMap.MAP_TYPE_NORMAL)
            .camera(CameraPosition.fromLatLngZoom(new LatLng(47.384906, 15.093149), 25));

    //supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    if(supportMapFragment == null) {
        supportMapFragment = SupportMapFragment.newInstance(options);
        getSupportFragmentManager().beginTransaction().replace(R.id.map, supportMapFragment).commit();
    }
  }
}


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="at.ac.unileoben.infotech.paapp.MainActivity"
tools:ignore="MissingPrefix">

  <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.SupportMapFragment" />
</FrameLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="at.ac.unileoben.infotech.paapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="at.ac.unileoben.infotech.paapp.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<permission android:name="at.ac.unileoben.infotech.paapp.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>

<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="at.ac.unileoben.infotech.paapp.MainActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="at.ac.unileoben.infotech.paapp.SettingsActivity"
        android:label="@string/action_settings" />

    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="XXX" />
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
  </application>

</manifest>

修改

我将main_activity的代码更改为以下内容..

public class MainActivity extends FragmentActivity {

private ArrayList<GPSCoordinates> list;
private static final String TAG = "MAIN_ACTIVITY";
private static Context context;
private MyLocationListener locListener;
private SupportMapFragment supportMapFragment;
private GoogleMap gmap;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setUpMap();
    list = new ArrayList<GPSCoordinates>();
    // access context global
    MainActivity.context = getApplicationContext();

    new Object();
}

@Override
protected void onStart() {
    super.onStart();
    if (gmap == null) {
        gmap = supportMapFragment.getMap();
        Log.i(TAG, "gmap set!");
    }
    if(locListener == null) {
        this.initLocationManager();
    }
    Log.i(TAG, "gmap start!");
}

@Override
protected void onResume() {
    super.onResume();
    Log.i(TAG, "gmap resume!");
}

private void setUpMap() {
    if(supportMapFragment == null) {
        supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    }
}

稍后我用以下方法调用我的simulatelocationmanager:

private void initLocationManager() {
    locListener = new MyLocationListener(gmap);
    // Define a listener that responds to location updates

    // Register the listener with the Location Manager to receive location updates
    SimulateLocationManager simulatedLocationManager = new SimulateLocationManager();

    //LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    simulatedLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, locListener);
    Log.i(TAG, "locListener");
}

在locationmanager中我调用函数viewlocation onlocationchanged

public void viewLocation(double latitude, double longitude)
{
    float bearing = this.bearing();
    Log.i(TAG2, "bearing" + bearing);
    gmap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
            .target(new LatLng(latitude, longitude))
            .bearing(bearing)
            .tilt(55f)
            .zoom(gmap.getCameraPosition().zoom).build()));
}

正如我之前所说的,问题是瓷砖没有显示,直到我点击地图.. 所有其他的东西似乎都有效,因为我可以看到相机在移动。但为什么瓷砖没有加载?

谢谢你的回复!

1 个答案:

答案 0 :(得分:0)

已经接受的答案是关于您的问题:Find here

如果您通过布局中的SupportMapFragment元素设置<fragment>,则可以getMap()成功呼叫onCreate()。但是,如果您通过构造函数创建SupportMapFragment,那太快了 - GoogleMap尚不存在。您可以延长SupportMapFragment并覆盖onActivityCreated(),因为getMap()已准备就绪。

但是,getMap()也可以针对更大的问题返回null,例如未安装Google Play服务。您需要使用GooglePlayServicesUtil.isGooglePlayServicesAvailable()之类的东西来检测这种情况并按照您的意愿处理它。