Google Map API v2 is not showing Map on device

*爱你&永不变心* 提交于 2019-11-30 06:02:43

Use Fragment activity

eg:

public class Maps extends FragmentActivity {
    GoogleMap map;
    double lat;
    double lan;
    boolean flag = false;

    // private LocationManager lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapptry);

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

Change ur map.xml to

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

EDIT

Got to do this also

import android.support.v4.app.FragmentActivity;

Before you do this Right click project->properties->buildpath->java build path -> libraries .. then click on add external jars

the go to

user\android-sdks\extras\android\support\v4

and select android-support-v4.jar

PS: Do all this provided your API key is correct . If you API key is wrong then also it shows only a white screen

try to add

GoogleMap googleMap =    ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

and replace your xml with

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
/>

you have added the following code outside application tag instead put it inside the tag

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

<permission
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

hope this might help someone who is facing the same problem

May be this problem with API key or you just check your SHA1 fingerprint in your project api console(https://console.developers.google.com/project) and your eclipse Windows -> Preferences -> Android -> Build are same, other wise just update the SHA1 from console to build path or vice versa.

Most probably your problem will be solved when you make sure below mentioned things:

  1. Activate Google Map API

  2. Use correct package name while generating API Key (in my case this was the mistake) .. and this seems problem to your case also as package name appears to be wrong (com.example.mapdemo)

  3. Make Sure you are using the correct keystore file for generating the SHA1

Hope this will be helpful.

To maintain security for the system and users, Android requires apps to request permission before the apps can use certain system data and features. Depending on how sensitive the area is, the system may grant the permission automatically, or it may ask the user to approve the request.

https://developer.android.com/guide/topics/permissions/index.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!