【问题标题】:Not able to show Google Map inside the fragment无法在片段内显示谷歌地图
【发布时间】:2020-11-15 17:51:59
【问题描述】:

我正在学习如何在我的应用程序上显示谷歌地图。目前,我已经完成了所有工作,包括添加元数据标签和生成 API 密钥,但是当我运行我的应用程序时,它只显示一个空片段。没有下载地图.

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

public static final int PERMISSION_REQUEST_CODE = 9001;
private static final int PLAY_SERVICES_ERROR_CODE = 9002;
public static final String TAG = "MapDebug";
private boolean mLocationPermissionGranted;

private GoogleMap mGoogleMap;


@RequiresApi(api = Build.VERSION_CODES.M)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);


    SupportMapFragment supportMapFragment= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
    supportMapFragment.getMapAsync(this);


    //isServicesOk();
    //initGoogleMap();

   // SupportMapFragment supportMapFragment=SupportMapFragment.newInstance();
   // supportMapFragment.getMapAsync(this::onMapReady);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(this, "Showing map", Toast.LENGTH_SHORT).show();
}

Xml 布局

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

【问题讨论】:

  • 有几个方法,你检查过这个link

标签: android google-maps supportmapfragment


【解决方案1】:

添加

android:name="com.google.android.gms.maps.SupportMapFragment"

在你的 xml 喜欢

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

编辑

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

欲了解更多信息,请访问https://developers.google.com/maps/documentation/android-sdk/start#step_5_hello_map_take_a_look_at_the_code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2020-02-14
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多