首页 > 解决方案 > 谷歌地图使用 SupportMapFragment 和 FragmentActivity 错误

问题描述

我很不幸地进入了一个 7 年的谷歌地图代码,它仍在使用 V1 地图 API。我计划至少将代码更改为 V2(目前)。所以在 StackOF 的任何地方我都发现了这个 -

在下面转换 -

com.google.android.maps.MapActivity --> android.support.v4.app.FragmentActivity

com.google.android.maps.MapView --> com.google.android.gms.maps.SupportMapFragment

MapView.getController() 和 MapView.getProjection() 等----> com.google.android.gms.maps.GoogleMap 对象

现在看看我的代码设计 -

下面是我的 XML 文件 -

                      <com.my.own.package.GoogleMapView
                            android:id="@+id/googleMapView"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:enabled="true"
                            android:clickable="true"
                            android:name="com.google.android.gms.maps.SupportMapFragment"
                            android:apiKey="XXXXX">
                       </com.my.own.package.GoogleMapView>

我的 GoogleMapView 类如下所示 -

public class GoogleMapView extends MapHelperGoogleMapView implements ViewContextMenuHandler {}

MapHelperGoogleMapView 如下所示 -

公共类 MapHelperGoogleMapView 扩展 SupportMapFragment {}

活动如下所示 -

public class GPSInfoTabActivity extends MapBridgeMapActivity
{
   GoogleMap myMap;
   GoogleMapView googleMapView = (GoogleMapView) getSupportFragmentManager().findFragmentById(R.id.googleMapView); // Question 1: Is this Casting correct ? Will this be properly casted ?
   googleMapView.getMapAsync(new OnMapReadyCallback() {
         @Override
         public void onMapReady(GoogleMap googleMap) {
             gMap = googleMap; // Question 2: I want to use this gMap object to modify Map like markers, to be used in My GoogleMapView and MapHelperGoogleMapView both of class

         }
   });
   //registerForContextMenu( googleMapView ); Question 3: This Peice of code Needed ? It came from V1 code. 
} 

有很多覆盖、绘图和 lof GeoPoints 正在使用,只有当我可以访问 GoogleMap 对象到我的自定义类中时,我才能移动该部分。但目前没有任何工作。有人可以指导我吗。如果您有任何疑问以了解我的问题,请发表评论。预先感谢。

标签: androidgoogle-maps

解决方案


在build.gradle中更新您的谷歌地图 Gradle 。

   implementation 'com.google.android.gms:play-services-maps:17.0.0'

在 AndroidManifest.xml 中,添加以下元素作为该元素的子元素

       <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_API_KEY"/>

在 xml 布局中添加此元素

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.android.app.MapsActivity" />

在 Java 代码中

public class GPSInfoTabActivity extends MapBridgeMapActivity implements OnMapReadyCallback
{

    private GoogleMap myMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
} 

 @Override
    public void onMapReady(GoogleMap map) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.getUiSettings().setZoomControlsEnabled(true);
        myMap = map;

    }

按照此参考链接获取更多信息。


推荐阅读