【问题标题】:Getting Google MapView V2 to work让 Google MapView V2 工作
【发布时间】:2014-07-27 22:21:43
【问题描述】:

有人能够让 Google MapView V2 工作吗?我浏览了 StackOverflow(包括这个问题:Android MapView getMap() returns null),每个人似乎都无法使用 MapView,最终不得不使用 Fragment。

具有以下 google_map_view.xml:

<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

我尝试以这种方式实例化地图:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );           
googleMapView = (View) layoutInflater.inflate(R.layout.google_map_view, null);
MapView mapView = (MapView)googleMapView.findViewById(R.id.map_view);
GoogleMap googleMap = mapView.getMap();

googleMap 将为空。

Google Play 服务与应用程序正确关联,并且我在 AndroidManifest.xml 文件中拥有正确的密钥。

想法?

【问题讨论】:

  • 在尝试检索GoogleMap 之前,尝试在MapView 上调用onCreate(),看看是否有帮助。您也可以暂时尝试使用MapFragment——即使您不打算长期使用MapFragment,也可能有助于解决与地图V2 相关的其他应用程序的任何配置问题。
  • mapView.getMap() 再往下一点。就像在 onActivityStarted()
  • 我尝试了这两种方法,但没有任何乐趣。我将不得不使用 MapFragment 方法,即使它涉及到相当多的返工。谷歌将这种机制放在那里是非常令人恼火的,但它在看似最简单的情况下不起作用。谢谢大家的建议。

标签: android google-maps android-mapview android-maps-v2


【解决方案1】:

我也刚开始学习 Google Maps API V2,上周完成了地图:

在我的活动布局中,我希望地图出现在我刚刚写的位置:

    <fragment
        android:id="@+id/PickDestMap"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

可以放置在 LinearLayout 或您希望的任何其他布局中。

在 Activity Java 文件中:

然后在 onCreate:

//Get fragment from activity (which was created in above layout file)
SupportMapFragment smf = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.PickDestMap);

GoogleMap wMap = smf.getMap();

现在使用 wMap,您可以根据需要配置、编辑、修改地图。 假设您的密钥设置正确,播放服务正确导入,它应该可以立即运行。

出于测试目的,我刚刚删除了 Activity.java 中的所有代码,它运行良好。

【讨论】:

  • 您没有阅读我的帖子,出于多种原因,我选择不使用 Fragment。我已经在其他应用程序中成功使用了 Fragment 方法,但这种方法不适合这种情况。
【解决方案2】:

谷歌现在使用以下方法更方便地获取地图

myMapFragment.getMapAsync(new OnMapReadyCallback) {
     @Override
     public void onMapReady(GoogleMap googleMap) {
         myMap = googleMap;
     }
});

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2012-11-21
    • 2018-06-15
    • 2014-07-17
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2017-12-19
    相关资源
    最近更新 更多