Android MapView display empty

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

Manifest:




        
相关标签:
4条回答
  • 2021-02-08 14:06

    Use Map API V2 for google map in android. Visit this link may be helps you.

    It is better than old version and easier too.

    0 讨论(0)
  • 2021-02-08 14:13

    Per the documentation (as of today) you need to forward lifecycle callbacks to the MapView.
    A cleaner way to do this is to use LifecycleObserver. This works when the map is placed in an activity or fragment.
    You don't get onLowMemory() or onSaveInstanceState() but you could still forward those to the MapView.

    Here's a Kotlin example.

    import android.content.Context
    import android.util.AttributeSet
    import androidx.lifecycle.Lifecycle
    import androidx.lifecycle.LifecycleObserver
    import androidx.lifecycle.LifecycleOwner
    import androidx.lifecycle.OnLifecycleEvent
    import com.google.android.gms.maps.MapView
    
    class MyMapView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
    ) : MapView(context, attrs, defStyle), LifecycleObserver {
    
        init {
            if(context is LifecycleOwner) {
                context.lifecycle.addObserver(this)
            }
        }
    
        //Your code here
    
        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        fun create() {
            onCreate(null)
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        fun start() {
            onStart()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        fun resume() {
            onResume()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        fun pause() {
            onPause()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        fun stop() {
            onStop()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        fun destroy() {
            onDestroy()
        }
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-08 14:14

    I guess you are making a mess from the version of Google Maps you are trying to implement. From the code that is used by you it's seems that you are trying to use Google Maps API V1.

    The problem with that is that you can't produce now days a new API key for Google Map API V1, this version is deprecated and Google doesn't provide new keys for it.

    from the comments it's looks that in the API Console you didn't activated the right API. Take a look at this blog post to get an Idea of how to produce an API Key for Google Map API V2 for Android:

    Google Maps API V2 Key

    Next, go over the following guide to implement this version in you application:

    Google Maps API V2

    0 讨论(0)
提交回复
热议问题