无法在片段内显示 Google 地图
Not able to show Google Map inside the fragment
我正在学习如何在我的 app.Currently 上显示 google 地图,我已经完成了所有工作,包括添加元数据标签和生成 API 密钥,但是当我 运行 我的应用程序只显示一个空的 fragment.No 地图已下载。
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 Layout
<?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>
添加
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));
}
我正在学习如何在我的 app.Currently 上显示 google 地图,我已经完成了所有工作,包括添加元数据标签和生成 API 密钥,但是当我 运行 我的应用程序只显示一个空的 fragment.No 地图已下载。
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 Layout
<?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>
添加
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));
}