【发布时间】: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