简体   繁体   English

Google Map未加载Android-Java

[英]Google Map is not loading Android - Java

Here is what I have but my map is not loading and onMapLoaded is not being called 这是我所拥有的,但是我的地图没有加载并且onMapLoaded没有被调用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

     <com.google.android.gms.maps.MapView android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_marginTop="30dp"/>

</RelativeLayout>

And then in my class I have 然后在我班上

public class Activity extends Activity implements GoogleMap.OnMapLoadedCallback {

MapView mMap;
GoogleMap mGooleMap;

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

    mMap = (MapView) findViewById(R.id.mapView);
    mGooleMap = mMap.getMap();
}

@Override
public void onMapLoaded() {

    Log.i(TAG, "loaaded");
}
}

But the onMapLoaded is never called why? 但是onMapLoaded从来没有被称为为什么?

Thanks for the help 谢谢您的帮助

EDIT 编辑

my xml is now this 我的xml现在是这个

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

And I also have 我也有

public class Activity extends FragmentActivity {

In Google Documentation they have given an example Google Map Example Google文档中,他们给出了一个 Google Map示例示例

You can implement OnMapReadyCallback listener which is given in sample app of google maps and override the onMapReady method as follows:- 您可以实现在Google地图示例应用中提供的OnMapReadyCallback侦听器,并重写onMapReady方法,如下所示:-

public class YourCalssName extends AppCompatActivity implements OnMapReadyCallback {

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

     init(); //initialise MapFragment 
    .....
}

private void init() {
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); // Important to call this method for listener
}

@Override
    public void onMapReady(GoogleMap googleMap) {
        if (googleMap != null) {
            //Do whatever you want to do
        }
    }
}

And in your layout add Map Fragment as follows:- 然后在您的布局中添加地图片段 ,如下所示:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.google.android.gms.maps.SupportMapFragment" 
            android:layout_marginBottom="10dp"/>

</LinearLayout>

Have you provided the GoogleMap V2 key? 您提供了GoogleMap V2密钥吗? Along with the key, Google Map needs following permissions which you have to mention in your project's AppManifest.xml. 除了密钥,Google Map还需要以下权限,您必须在项目的AppManifest.xml中提及这些权限。

ACCESS_NETWORK_STATE – To check network state whether data can be downloaded or not ACCESS_NETWORK_STATE –检查网络状态是否可以下载数据
INTERNET – To check internet connection status 互联网 –检查互联网连接状态
WRITE_EXTERNAL_STORAGE – To write to external storage as google maps store map data in external storage ACCESS_COARSE_LOCATION – To determine user's location using WiFi and mobile > cell data ACCESS_FINE_LOCATION – To determine user's location using GPS OpenGL WRITE_EXTERNAL_STORAGE –以Google Maps的形式写入外部存储,将地图数据存储在外部存储中ACCESS_COARSE_LOCATION –使用WiFi和移动设备确定用户的位置>小区数据ACCESS_FINE_LOCATION –使用GPS OpenGL确定用户的位置
ES V2 – Required for Google Maps V2 ES V2 – Google Maps V2必需

If all the above mentioned requirements are fulfilled, this code snippet will help you. 如果满足上述所有要求,此代码段将为您提供帮助。 public class MainActivity extends Activity { 公共类MainActivity扩展了Activity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

PS Try that if(mGoogleMap == null) check to make sure that you do have an instance of Google map. PS尝试进行if(mGoogleMap == null)检查以确保您确实具有Google Map的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM