我想在地圖上添加標記。但initializeMap()
返回null。谷歌地圖對象爲空
可能是什麼問題?
ActivityPlace.java
public class ActivityPlace extends AppCompatActivity {
GoogleMap mMap;
LatLng latlng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place);
if (initializeMap() && checkServices()) {
latlng = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions().position(latlng);
Marker marker = mMap.addMarker(options);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latlng, 12);
mMap.animateCamera(update);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Launch Map Page
}
});
}
}
/*
Method to check Google services before initiating map
*/
public boolean checkServices() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS){
return true;
}else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
}else {
Toast.makeText(this, "Cant connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
/*
Initialize the map and get a handle to the map to set its location
*/
private boolean initializeMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
});
}
return (mMap != null);
}
}
activity_place.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/place_layout">
<TextView
style="@style/txtPrimaryHeading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="@+id/place_name"/>
<include
android:id="@+id/map"
layout="@layout/activity_map_embedded_fragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_map_embedded_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:uiRotateGestures="false"
map:uiTiltGestures="false"
map:uiScrollGestures="false"
map:uiZoomGestures="false"
map:cameraZoom="12"
map:cameraTargetLat="-35"
map:cameraTargetLng="150"
/>
顯然是因爲'getMapAsync' ...你看到** async?** ...它是[多線程基礎知識](http://ideone.com/PPHi95) – Selvin