Gonzalo1987
Gonzalo1987

Reputation: 87

Google Maps Android API v2 not show the map

I've a problem to show a map using the Google Map Android API v2.

The app debug dont show any error but when I call the Activity I get a blank MapView with no map inside.

My code is:

activity_map.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
>

    <com.google.android.gms.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout> 

MapViewerActivity

public class MapViewerActivity extends Activity {

    MapView mapView;
    GoogleMap map;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_map);

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);

    MapsInitializer.initialize(this);

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);
}

Any idea? Tyvm for read!

Upvotes: 1

Views: 1187

Answers (2)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

Try this way,

activity_map

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapViewerActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

MapViewerActivity

public class MapViewerActivity extends Activity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }
}

Finally make sure you have added the following features & permissions in manifest,

        <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true"/>

  <!-- Creating Permission to receive Google Maps -->
    <permission
        android:name="com.example.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <!-- Permission to receive Google Maps -->
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />

     <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="@string/google_maps_key" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

          <uses-permission android:name="android.permission.INTERNET"/>
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
          <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Make sure you are using a correct API key and your Google Maps v2 is enabled in API console.

Upvotes: 0

Andrew Brooke
Andrew Brooke

Reputation: 12173

From Google's documenation:

To use the Google Maps Android API v2, you must register your app project on the Google Developers Console and get a Google API key which you can add to your app. The type of API key you need is an Android key.

Follow Google's instructions here to set up a maps API key

https://developers.google.com/maps/documentation/android/signup

In short:

  • Make a project in the Google Developers Console
  • Generate a key to use the Maps API
  • Add the key to your android manifest

Upvotes: 1

Related Questions