I'm trying to show custom marker with images
here images are download from internet.
Everything is fine getting result as expected.
Here is the output image
But it is taking lot of time to show maps here it is downloading images and then showing maps. Is there any way to show maps
first and then download images and set markers
.
Here is the code i tried
@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));
}
}
I'm using asynctask
to download the images here is the code for that
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();
}
}
The GoogleMap
object has a method to set a callback for when the map is loaded. Add the following in your onMapReady
method:
mMap.setOnMapLoadedCallback(this);
and have a method in your class that then implements the callback:
@Override
public void OnMapLoaded() {
AddMarkers addMarkersTask = new AddMarkers(url);
addMarkersTask.execute();
}