'How to add a marker in my location in a map
I want to add an image into my map marker, I don't know why this code is not working. The marker is not displaying in my current location.If I move the marker to the onMap ready and assign the location to the LatLng one or two, it will work but when i move it to the OnLocationChanged, it will not work. I want to use it in the onLocationChanged Here is the code.
onMapready
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng one = new LatLng(4.9757,8.3417);
LatLng two = new LatLng(4.9857,8.4417);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(one);
builder.include(two);
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.20);
mMap.setLatLngBoundsForCameraTarget(bounds);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding));
mMap.setMinZoomPreference(mMap.getCameraPosition().zoom);
buildGoogleApiClient();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
@Override
public void onLocationChanged(Location location) {
BitmapDescriptor imageDrawable = BitmapDescriptorFactory.fromResource(R.drawable.driver);
if (lastLocation !=null) {
lastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(new MarkerOptions().position(latLng).title("My Location")
.icon(imageDrawable));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|