【发布时间】:2014-01-17 02:33:50
【问题描述】:
我正在尝试让地图在 Fragments 中工作,但我得到“方法 getSupportFragmentManager() 未在下面的代码中为 Karta 类型定义。 我也尝试过使用 getFragmentManager 而不是 getSupportFragmentManager,但后来我得到“无法从 Fragment 投射到 MapFragment”。
Karta.Java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdate;
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.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Karta extends SupportMapFragment{
public static Karta newInstance() {
Karta f = new Karta();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.karta, container, false);
System.out.println("OnCreateView");
return v;
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState){
super.onViewCreated(v, savedInstanceState);
System.out.println("onViewCreated1");
final LatLng BUTIKPLATS = new LatLng(57.873873, 11.974995);
System.out.println("onViewCreated2");
GoogleMap karta;
karta = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.karta)).getMap();
karta.addMarker(new MarkerOptions().position(BUTIKPLATS).title("Vita Fläckens Blommor"));
karta.setMapType(GoogleMap.MAP_TYPE_HYBRID);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(BUTIKPLATS, 17);
karta.animateCamera(update);
}
}
karta.XML
<?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"
android:background="@color/BGColor"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/karta"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
【问题讨论】:
标签: android google-maps android-fragments