Search code examples
androidandroid-mapviewandroid-maps

google map not working on both emulator and device


I am trying to run a simple google map but its not working its showing only map background.checked on both emulator and device.

xml file:-

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

Java class

import android.os.Bundle; import
com.google.android.gms.maps.GoogleMap; import
com.google.android.gms.maps.SupportMapFragment; import
com.google.android.gms.maps.model.BitmapDescriptorFactory; import
com.google.android.gms.maps.model.LatLng; import
com.google.android.gms.maps.model.Marker; import
com.google.android.gms.maps.model.MarkerOptions;

public class LocationGB extends android.support.v4.app.FragmentActivity {     
    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;
     @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

        if (map!=null){
            Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher))); 
        }
    } 
}

Solution

  • In your manifest file you have to add:

    <permission
      android:name="your.application.package.permission.MAPS_RECEIVE"
      android:protectionLevel="signature" />
    

    Like from: your.application.package to: com.themontcalm.droid

    And yes if you are using

    <uses-library android:name="com.google.android.maps" />
    

    then remove from manifest file:

    and add your api code to in manifest file also:

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

    And in manifest file you are missing this:

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

    so also add to this also in manifest file:

    you should change android.R.id.content with R.id.map in your code

    Something like:

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();