I have an Activity that, when it loads, will load and display a map fragment. The problem is when the activity opens, an empty map is displayed. Then a few seconds later, the map is updated to be in the correct position as given by myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 4));
How do I make it so that the map will only display once the correct position has been set by the moveCamera
line?
Here is the code for my Activity:
public class actWhereBeen extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap myGoogleMap;
private static String TAG = "actWhereBeen";
Context context;
clsSqliteHandler mySqliteHandler = new clsSqliteHandler();
ProgressBar progressBar;
SQLiteDatabase sqLiteDatabase;
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_where_been);
context = this;
sqLiteDatabase = openOrCreateDatabase("wildlife_db",MODE_PRIVATE,null);
addMapFragment();
// progressBar = findViewById(R.id.progressBar);
////
// progressBar.setVisibility(View.VISIBLE);
////
// locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
//
// dblCrntLat = -27.8885;
// dblCrntLong = 32.7777;
//
// SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapWhere);
// supportMapFragment.getMapAsync(this);
}
private void addMapFragment() {
mMapFragment.getMapAsync(this);
getSupportFragmentManager().beginTransaction()
.replace(R.id.mapWhere, mMapFragment)
.commit();
}
@Override
public void onMapReady(GoogleMap googleMap) {
myGoogleMap = googleMap;
Region region = new Region();
region = region.getRegionFromKey(sqLiteDatabase,1);
Double dblLatCenter = region.getCenter_lat();
Double dblLongCenter = region.getCenter_long();
LatLng marker = new LatLng(dblLatCenter, dblLongCenter);
myGoogleMap.getUiSettings().setRotateGesturesEnabled(false);
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 4));
Cursor csrSightings = getSightingsCursor();
addMarkersToMap(csrSightings);
}
public Cursor getSightingsCursor() {
// an array for the filter values
List<String> arrWhere = new ArrayList<String>();
String[] finalValue = new String[ arrWhere.size() ];
arrWhere.toArray( finalValue );
// an array for the columns to return
List<String> arrCols = new ArrayList<String>();
arrCols.add("latitude");
arrCols.add("longitude");
Cursor csrCoords = mySqliteHandler.getCursorAllPurpose(false, "tbl_fact_sightings", arrCols, "", arrWhere,
null, null, null);
return csrCoords;
}
public void addMarkersToMap(Cursor csrSightings){
int intCount = csrSightings.getCount();
if (intCount > 0) {
for( csrSightings.moveToFirst(); !csrSightings.isAfterLast(); csrSightings.moveToNext() ) {
Double dblLat = Double.valueOf(csrSightings.getString(0));
Double dblLong = Double.valueOf(csrSightings.getString(1));
populateMap(dblLat, dblLong);
}
}
}
public void populateMap(Double dblLat, Double dblLong){
// Create a mutable bitmap
Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
bitMap = bitMap.copy(bitMap.getConfig(), true);
// Construct a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(bitMap);
// Create a new paint with default settings.
Paint paint = new Paint();
// smooths out the edges of what is being drawn
paint.setAntiAlias(true);
// set color
paint.setColor(Color.BLUE);
// set style
paint.setStyle(Paint.Style.FILL);
// set stroke
paint.setStrokeWidth(4.5f);
// draw circle with radius 30
canvas.drawCircle(50, 50, 15, paint);
LatLng marker = new LatLng(dblLat, dblLong);
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
myGoogleMap.addMarker(new MarkerOptions().title("Test")
.position(marker)
.icon(BitmapDescriptorFactory.fromBitmap(bitMap)
));
}
}
Create marker icon bitmap only once.
public class actWhereBeen extends AppCompatActivity implements OnMapReadyCallback {
Bitmap mMarkerIcon; // "global" variable
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMarkerIcon = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
mMarkerIcon = mMarkerIcon.copy(mMarkerIcon.getConfig(), true);
// Construct a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(mMarkerIcon);
// Create a new paint with default settings.
Paint paint = new Paint();
// smooths out the edges of what is being drawn
paint.setAntiAlias(true);
// set color
paint.setColor(Color.BLUE);
// set style
paint.setStyle(Paint.Style.FILL);
// set stroke
paint.setStrokeWidth(4.5f);
// draw circle with radius 30
canvas.drawCircle(50, 50, 15, paint);
...
}
}
In populateMap()
just set previously created bitmap:
LatLng marker = new LatLng(dblLat, dblLong);
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
myGoogleMap.addMarker(new MarkerOptions().title("Test")
.position(marker)
.icon(BitmapDescriptorFactory.fromBitmap(mMarkerIcon)
));
Remove moveCamera()
call, line:
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
from public void populateMap(Double dblLat, Double dblLong)
method:
public void populateMap(Double dblLat, Double dblLong){
LatLng marker = new LatLng(dblLat, dblLong);
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3)); // <- this line should be removed
myGoogleMap.addMarker(new MarkerOptions().title("Test")
.position(marker)
.icon(BitmapDescriptorFactory.fromBitmap(bitMap)
));
}
This is not all of optimization, but the direction for improve your code.