2016-11-09 47 views
-2

我想在地图上添加标记。但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" 
    /> 
+0

显然是因为'getMapAsync' ...你看到** async?** ...它是[多线程基础知识](http://ideone.com/PPHi95) – Selvin

回答

1

getMapAsync是将在后台运行的异步调用,并且您的方法将仅返回值false。

我的建议是在调用onMapReady时执行其他操作。

mapFragment.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     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 
      } 
     }); 
    } 
}); 
0

在初始化映射之前调用它。

GoogleApiClient mGoogleApiClient ; 


synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 

} 
0

试试这个。真的行。

GoogleApiClient mGoogleApiClient ; 


synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 

}