【问题标题】:Google Map Using SupportMapFragment and FragmentActivity Errors谷歌地图使用 SupportMapFragment 和 FragmentActivity 错误
【发布时间】:2019-07-04 15:26:45
【问题描述】:

很遗憾,我使用了一个 7 年前的 Google Map 代码,该代码仍在使用 V1 Map 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 {}

而 Activity 如下所示 -

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 对象到我的自定义类中时,我才能移动这部分。但目前没有任何工作。有人可以指导我吗。如果您有任何疑问以了解我的问题,请发表评论。预先感谢。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    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;
    
        }
    

    关注此参考link 以获取更多信息。

    【讨论】:

    • 感谢您的代码,但直到这一部分,我已经完成了,唯一的问题是如何在我的两个类 GoogleMapView 和 MapHelperGoogleMapView 中使用 myMap 和 mapFragments 对象,因为它们有很多旧的逻辑绘图、Canvas 和 getProjection、getController 逻辑。因为您更改了 XML 文件,但没有提及 com.my.own.package.GoogleMapView 。我提出问题是因为这个文件有很多逻辑。
    • 能否请您在这里写下您的 GoogleMapView 和 MapHelperGoogleMapView 的代码?所以我可以就你需要做什么提出建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2016-05-09
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多