我试图展示
custom marker with images
,这里的图片是从互联网下载的。
一切都很好,得到了预期的结果。
这是输出图像
但是这里显示地图需要花费很多时间,它是下载图像然后显示地图。有没有办法先显示
maps
,然后下载图片并设置markers
。
这是我尝试过的代码
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
MapsInitializer.initialize(this);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
assert locationManager != null;
String provider = locationManager.getBestProvider(criteria, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}, LOCATION_REQUEST_CODE);
return;
}
}
mLastLocation = locationManager.getLastKnownLocation(provider);
userLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLatLng, 14.0f));
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setBuildingsEnabled(true);
try {
businessBitmap = new AddMarkers(imgUrl2).execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (businessLatLng != null) {
businessMarker = mMap.addMarker(new MarkerOptions()
.position(businessLatLng)
.title(businessUser)
.snippet(getLocation(businessLatLng))
.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(businessBitmap))));
} else {
businessMarker = mMap.addMarker(new MarkerOptions()
.position(businessLatLng)
.title(businessUser)
.snippet(getLocation(businessLatLng)));
}
try {
userBitmap = new AddMarkers(imgUrl).execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (mLastLocation != null) {
if (userBitmap != null) {
userMarker = mMap.addMarker(new MarkerOptions().position(userLatLng)
.title(username)
.snippet(getLocation(userLatLng))
.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(userBitmap))));
} else {
userMarker = mMap.addMarker(new MarkerOptions().position(userLatLng)
.title(username)
.snippet(getLocation(userLatLng)));
}
}
if(!(userLatLng == null || businessLatLng == null)) {
Polyline distanceLine = mMap.addPolyline(new PolylineOptions().add(userLatLng, businessLatLng).width(4).color(Color.GREEN));
}
}
我正在使用
asynctask
下载图像,这里是代码
public AddMarkers(String imgUrl) {
this.imgUrl = imgUrl;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MapsActivity.this);
pd.setTitle("Loading");
pd.setMessage("Please wait..");
pd.show();
}
@Override
protected Bitmap doInBackground(Void... voids) {
URL url;
try {
url = new URL(imgUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (pd != null && pd.isShowing()) pd.dismiss();
}
}
GoogleMap
对象有一个方法可以在加载地图时设置回调。在您的 onMapReady
方法中添加以下内容:
mMap.setOnMapLoadedCallback(this);
并在你的类中有一个方法来实现回调:
@Override
public void OnMapLoaded() {
AddMarkers addMarkersTask = new AddMarkers(url);
addMarkersTask.execute();
}