【问题标题】:Android Map V2 map load in one activity but not in other twoAndroid Map V2 地图在一个活动中加载,但在其他两个活动中不加载
【发布时间】:2013-08-18 12:35:57
【问题描述】:

我是 android map api v2 的新手。我正在尝试在 3 个不同的活动中(在某些用户操作上)一一加载地图。地图在 1 个活动中加载良好,但仅显示带有缩放按钮的灰色框。

以下是 XML 布局(map_view.xml),我使用 include 标签将其包含在三个不同的布局中。

<?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" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp" >

    <fragment
        android:id="@+id/mapLocationDetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

    <ImageView
        android:id="@+id/transparent_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@android:color/transparent" />

    <Button
        android:id="@+id/btnRefresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/btn_green_small"
        android:text="Direction"
        android:textColor="@color/white" />
</RelativeLayout>

以下是在一项活动中正确加载地图的代码。但在其他 2 中没有。

package com.nthreads.out2b.activity.places;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.nthreads.out2b.Out2B;
import com.nthreads.out2b.R;

public class LocationDetailActivity extends Out2B {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;

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

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapLocationDetail))
            .getMap();
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.map_resturant_marker)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    map.getUiSettings().setZoomControlsEnabled(false);
}

地图加载正确:

加载不正确:

【问题讨论】:

  • 我认为您提供的代码运行良好。地图不工作的Activity的代码在哪里?

标签: android eclipse google-maps


【解决方案1】:

我找到了这个问题的一些替代方案。您的代码看起来不错,代码或包含标签没有问题。主要问题是标签,我猜您的地图在您将其加载到活动中而不是在标签中工作的情况下工作正常。所以我建议使用视图寻呼机或制作自己的控件以显示为选项卡。地图将以这种方式工作。

【讨论】:

    【解决方案2】:
    @Override
    public void onDestroy() {
        super.onDestroy();
        Fragment fragment = getFragmentManager().findFragmentById(R.id.mapLocationDeatail);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }
    

    在这些活动中试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多