如何在显示地图之前获取要加载的标记和 newLatLngZoom 位置?
How to get Markers and newLatLngZoom position to load before Map is displayed?
我有一个 Activity,它在加载时会加载并显示地图片段。问题是当 activity 打开时,会显示一张空地图。然后几秒钟后,地图更新为 myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 4));
给出的正确位置
如何才能使地图仅在 moveCamera
行设置正确位置后显示?
这是我的 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)
));
}
}
只创建一次标记图标位图。
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);
...
}
}
在populateMap()
中刚刚设置了之前创建的位图:
LatLng marker = new LatLng(dblLat, dblLong);
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
myGoogleMap.addMarker(new MarkerOptions().title("Test")
.position(marker)
.icon(BitmapDescriptorFactory.fromBitmap(mMarkerIcon)
));
删除 moveCamera()
调用,行:
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
来自public void populateMap(Double dblLat, Double dblLong)
方法:
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)
));
}
这不是优化的全部,而是改进代码的方向。
我有一个 Activity,它在加载时会加载并显示地图片段。问题是当 activity 打开时,会显示一张空地图。然后几秒钟后,地图更新为 myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 4));
如何才能使地图仅在 moveCamera
行设置正确位置后显示?
这是我的 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)
));
}
}
只创建一次标记图标位图。
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); ... } }
在
populateMap()
中刚刚设置了之前创建的位图:LatLng marker = new LatLng(dblLat, dblLong); myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3)); myGoogleMap.addMarker(new MarkerOptions().title("Test") .position(marker) .icon(BitmapDescriptorFactory.fromBitmap(mMarkerIcon) ));
删除
moveCamera()
调用,行:
myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 3));
来自public void populateMap(Double dblLat, Double dblLong)
方法:
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)
));
}
这不是优化的全部,而是改进代码的方向。