Android MapView display empty

前端 未结 4 1931
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 13:16

Manifest:




        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 14:14

    The way I got mine resolved, I had to do this:

    final MapView mapView = (MapView)fragmentView.findViewById(R.id.map_fieldLocation);
    
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
            googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
            mapView.onResume();
        }
    });
    

    The important part that I was missing is that you have to call the onCreate() method before you call getMapAsync() and once the callback is called, you need to call onResume() on the MapView object.

    That totally solved it for me.

    Here's what it would look like in your own class:

    public class MainActivity extends MapActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            if (getView() != null) {
    
                final MapView mapView = (MapView)getView().findViewById(R.id.mapView);
    
                mapView.onCreate(savedInstanceState);
                mapView.getMapAsync(new OnMapReadyCallback() {
    
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
                        googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
                        mapView.onResume();
                    }
                }
            }
        }
    
        @Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }
    
    }
    

    Hope this helps!

提交回复
热议问题