【问题标题】:GoogleMap getMapAsync wont work in fragmentGoogleMap getMapAsync 在片段中不起作用
【发布时间】:2017-05-21 15:25:46
【问题描述】:

我正在尝试在我的项目中实现 googleMaps,我只是添加了一个新片段和以下代码。目前我可以在我的项目中显示地图,但添加标记不起作用,mapFragment.getMapAsync(this) 给出以下错误消息。无法解析方法'getMapAsync(testCodes.project2.MapFragment)

public class MapFragment extends Fragment implements OnMapReadyCallback {
    private GoogleMap mMap;
//    private MapView mapView;
    private Boolean mapReady = false;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    public MapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);//remember getMap() is deprecated!


//    mapFragment.getMapAsync(this);
        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng sydney = new LatLng(-33.852, 151.211);

        MarkerOptions markerOptions = new MarkerOptions().position(sydney).title("Random position");
        googleMap.addMarker(markerOptions);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        checkPermission("Permission", 1, 1);
        mMap.setMyLocationEnabled(true);
    }


}

片段的xml。

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

【问题讨论】:

  • 你需要添加googleservice.json。希望您在 Manifest 中也添加了所需的内容
  • 我已经完成了所需的事情,例如获取 google sdk、api 密钥等。但不确定您对 googleservice.json 的意思。 android:name="com.google.android.geo.API_KEY" android:value="myKey"/>
  • 试试这个链接。它将指导您如何操作:stackoverflow.com/questions/34367870/…

标签: java android google-maps android-fragments


【解决方案1】:

在 xml 文件中,您使用的是 class="com.google.android.gms.maps.SupportMapFragment",所以在 MapFragment.java 中您必须使用SupportMapFragment 而不是 MapFragment

改变这个:

MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);//remember getMap() is deprecated!

到:

            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);//remember getMap() is deprecated!

【讨论】:

  • 当我尝试这个时,我在声明中收到以下错误:Inconvertible types; cannot cast 'android.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment
  • 尝试将您的导入从 android.app.Fragment 更改为 android.support.v4.app.Fragment
猜你喜欢
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 2017-09-03
  • 2020-02-03
  • 2014-10-02
相关资源
最近更新 更多