2016-06-30 55 views
0

我在Android中使用谷歌地图v2时遇到了一个问题。我想要一个MapView作为布局的一部分。我可以将MapView与标记一起绘制,但是除非我点击地图,否则瓷砖本身不会加载。对于每次点击我都会绘制一个额外的图块(因此需要点击4或5次来获取整个地图)。谷歌地图v2瓷砖仅在Android上的点击后绘制

我正在使用Maps V2,但在这种特殊情况下,不在片段中。作为说明,地图在我使用MapFragment的另一个地方可以正常工作。

以下是代码的重要片段:

片段活性的:

布局
public class ViewNodeDetailsGame extends MapActivity implements View.OnClickListener, OnMapReadyCallback { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     mBundle = savedInstanceState; 

     setContentView(R.layout.activity_view_node_details_game); 

     // Maps related 
     MapsInitializer.initialize(this); 

     // ... the data is fetched asynchronously here. Then, getLocation() is called 
    } 


    private void getLocation(final LinearLayout llLocation) { 

     mMapView = (MapView)llLocation.findViewById(R.id.map); 
     mMapView.onCreate(mBundle); 
     mMapView.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 

     LatLng sydney = new LatLng(-34, 151); 
     map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     map.animateCamera(CameraUpdateFactory.newLatLng(sydney)); 

     map.getUiSettings().setMyLocationButtonEnabled(true); 
     map.getUiSettings().setZoomControlsEnabled(true); 
     map.getUiSettings().setCompassEnabled(true); 
     map.getUiSettings().setAllGesturesEnabled(true); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     if (mMapView != null) { 
      mMapView.onResume(); 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mMapView != null) { 
      mMapView.onPause(); 
     } 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     if (mMapView != null) { 
      mMapView.onLowMemory(); 
     } 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 
     if (mMapView != null) { 
      mMapView.onSaveInstanceState(outState); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestoy(); 
     if (mMapView != null) { 
       mMapView.onDestroy(); 
     } 
    } 

片段。这种布局以编程方式添加:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:padding="5dp" 
    android:orientation="vertical"> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/map" 
     android:clickable="true" 
     android:enabled="true" 
     android:layout_width="match_parent" 
     android:layout_height="250dp" /> 

    <!-- More stuff below --> 

我包括我的API_KEY在AndroidManifest.xml文件:

<!-- lots of stuff above this... --> 
    <meta-data 
    android:name="com.google.android.geo.API_KEY" 
    android:value="AIza --rest of my key here--" 
    /> 
</application> 

下图显示了瓷砖画我每次点击获得的进展。任何提示都表示赞赏。

enter image description here

回答

0

那是因为你没有在活动的的onResume()调用回调 mMapView.onResume()。

+0

是的,其实我是。我只是没有在片段中添加。我只是更新了代码。 – zundi

+0

getLoation()在onCreate()中调用还是从不同的callstack调用? – duanbo1983

+0

asynctask是在'onCreate()'中启动的,当完成检索数据时,'getLocation()'方法被调用。 – zundi