2016-08-16 40 views

回答

0

将其添加到XML中。

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

,并尝试使用该Java中的文件

private GoogleMap map; 
private void setUpMapIfNeeded() { 
// Do a null check to confirm that we have not already instantiated the 
// map. 
if (map == null) { 

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    map.setMyLocationEnabled(true); 
    map.getUiSettings().setMyLocationButtonEnabled(false); 
    map.getUiSettings().setZoomControlsEnabled(false); 
    map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 
     @Override 
     public void onMyLocationChange(Location loc) { 

     } 
    }); 
} 

}

使用此方法从API创建多个标记。

protected void createMarker(double latitude, double longitude, Bitmap iconResID) { 

    map.addMarker(new MarkerOptions() 
      .position(new LatLng(latitude, longitude)) 
        /* .icon(BitmapDescriptorFactory 
          .fromResource(R.drawable.pin_client_org))*/ 
      .icon(BitmapDescriptorFactory.fromBitmap(iconResID))); 
        /* .title(title) 
        .anchor(0.5f, 0.5f) 
        .snippet(snippet)*/ 

} 
+0

谢谢。但是。 ..仍然错误此行:无法解析方法getMap()map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap(); –

相关问题