2012-07-24 100 views
0

因此,我有一个应用程序必须显示多个商店的位置,具体取决于您选择哪一个。 用于与的MapView的页中的XML布局是一个ViewFlipper的一部分,并且具有地图布局的部分是:(Android)如何在mapview中设置默认GPS位置

<RelativeLayout 
    android:id="@+id/RelativeLayout3" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/back2" 
    android:gravity="left" > 

    <ImageButton 
     android:id="@+id/backLocation3" 
     android:layout_width="72dp" 
     android:layout_height="30dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:src="@drawable/btn_zuruck2" /> 

    <ImageView 
     android:id="@+id/imageViewMap" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/backLocation3" 
     android:layout_marginTop="14dp" 
     android:src="@drawable/businesslounge" /> 

    <RelativeLayout 
     android:id="@+id/mapPlaceHolderLayout" 
     android:layout_width="300dp" 
     android:layout_height="200dip" 
     android:layout_alignLeft="@+id/backLocation3" 
     android:layout_below="@+id/imageViewMap" 
     android:layout_marginTop="92dp" > 
    </RelativeLayout> 







    <TextView 
     android:id="@+id/mapLocation" 
     android:layout_width="240dp" 
     android:layout_height="40dp" 
     android:layout_alignLeft="@+id/mapPlaceHolderLayout" 
     android:layout_below="@+id/imageViewMap" 
     android:layout_marginTop="47dp" 
     android:text="@string/maplocation" 
     android:textSize="25dp" 
     android:textColor="@color/purple" /> 



    <ImageButton 
     android:id="@+id/phonereserve" 
     android:layout_width="220dp" 
     android:layout_height="36dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="24dp" 
     android:src="@drawable/rezervephone" /> 

</RelativeLayout> 

,涉及到的MapView代码的一部分是:

public class StandorteFragment extends MapHostFragment { 

public String text2; 
private RelativeLayout mapPlaceHolderLayout; 
public AQuery aQuery; 

private MapView myMapView; 
private GeoPoint geoPoint; 
private MapController mc; 

@Override 
protected Class<? extends Activity> getActivityClass() { 
    return LocationMapActivity.class; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.location, container, false); 
    ViewFlipperStandorte = (ViewFlipper) v.findViewById(R.id.viewFlipperLocation); 
    geoPoint = new GeoPoint((int) (48.949997* 1E6), (int) (22.140213 * 1E6)); 
    mapPlaceHolderLayout = (RelativeLayout)v.findViewById(R.id.mapPlaceHolderLayout); 
    mapPlaceHolderLayout.addView(onCreateMapView()); 
//  myMapView = (MapView) v.findViewById(R.id.mapPlaceHolderLayout); 
//  mc = myMapView.getController(); 
//  mc.animateTo(geoPoint); 
// 

它可以工作,但不会在mapView中不显示任何内容。如果我不使用myMapView和动画geoPoint评论这3行,它强制关闭。 我需要做什么?

编辑:onCreateMapView:

public View onCreateMapView() { 
    Intent intent = new Intent(getActivity(), getActivityClass()); 
    final Window w = getLocalActivityManager().startActivity(ACTIVITY_TAG, 
      intent); 
    final View wd = w != null ? w.getDecorView() : null; 

    if (wd != null) { 
     ViewParent parent = wd.getParent(); 
     if (parent != null) { 
      ViewGroup v = (ViewGroup) parent; 
      v.removeView(wd); 
     } 

     wd.setVisibility(View.VISIBLE); 
     wd.setFocusableInTouchMode(true); 
     if (wd instanceof ViewGroup) { 
      ((ViewGroup) wd) 
        .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
     } 
    } 
    return wd; 
} 
+0

您是否设置了Google Map API密钥? – HaemEternal 2012-07-24 13:14:57

+0

是的,地图现在可以运行,但不知道如何设置中心位置 – 2012-07-25 14:39:54

+0

这里有一些很好的地图教程: http://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android -development-tutorial-part-1/ 除此之外,它们还包括设置该教程帮助的中心点 – HaemEternal 2012-07-25 15:07:24

回答

0

mapPlaceHolderLayout.addView(onCreateMapView());称为LocalActivityManagerFragment的MapHostFragment以某种方式进入扩展MapActivity的LocationMapActivity,所以在这里我可以实例化MapController,并且所有东西都以普通形式出现

1

嘛R.id.mapPLaceHolderLayout在XML是不是一个MapView的,所以你不能把它转换为一个。该行会导致异常。

+0

好的,但我如何使用mapview?因为我知道mapPlaceHolderLayout是一个布局,加载地图。 – 2012-07-24 13:21:47

+0

onCreateMapView的代码是做什么的?请发布该代码。 – Kaediil 2012-07-24 13:25:41

+0

编辑它的问题 – 2012-07-24 13:27:58