How to set the markers icon? - android

I want to cluster my markers on the GoogleMaps map on my Android device. therefore, I tried this example:
public class GoogleMapClusterItem implements ClusterItem {
public final String name;
public final int profilePhoto; // resource id
public final LatLng mPosition;
public MapClusterItem(double lat, double lng, String name, int pictureResource) {
this.name = name;
profilePhoto = pictureResource;
mPosition = new LatLng(lat, lng);
}
#Override
public LatLng getPosition() {
return mPosition;
}
}
And then I call in the Main Activity:
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
lat = lat + offset;
lng = lng + offset;
MapClusterItem offsetItem = new MapClusterItem(lat, lng, "Casa Mia", R.drawable.ic_casa);
mClusterManager.addItem(offsetItem);
}
I would like to replace a markers icon with an individial png picture. But there is always the red standard marker symbol.
Where should I call the drawMarker Method then?
As you can see, these are not Marker objects.

private void drawMarker(LatLng point, int drawable)
{
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.icon(BitmapDescriptorFactory.fromResource(drawable));
markerOptions.position(point);
googleMap.addMarker(markerOptions);
}
You can use the above method to draw markers where param drawable is the drawable resource id (image) and point is LatLng where the marker has to be drawn.

Related

How to move google map marker smoothly between two location with waypoint in android

I need to move google map marker smoothly like this example. Also in my case has some waypoints additional to start and end location. Already I used below code. but it hasn't constant speed at all point.
public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {
Marker marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(0))
.flat(true));
Marker marker1 = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(10))
.flat(true));
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));
animateMarker(myMap, marker, marker1, directionPoint, false);
}
private static void animateMarker(GoogleMap myMap, final Marker marker, final Marker marker1, final List<LatLng> directionPoint,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = myMap.getProjection();
final long duration = (long) (30000);
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
int i = 0;
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
if (i < directionPoint.size()){
marker.setPosition(directionPoint.get(i));
marker1.setPosition(directionPoint.get(i+10));
}
i++;
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
Also I need to turn marker according to route between two location.

HelpAndroid map marker change start and end color, put different details and add polylines

Is there a way I can change the icon of start and end of marker like blue for start and red for end, add different details and add connecting polyline?
Button:
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < list_location.size(); i++) {
createMarker(list_location.get(i).getLatitude(), list_location.get(i).getLongitude());
}
}
});
Marker:
private void createMarker(String latitude, String longitude) {
// Adding the taped point to the ArrayList
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_camera);
Double lat = Double.parseDouble(latitude);
Double Longitude = Double.parseDouble(longitude);
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, Longitude))
.anchor(0.5f, 0.5f)
.title("title")
.snippet("snippet")
.icon(icon));
}
It's easy but seems you haven't tried much
you can use index to find first and last points
and draw different color based on position
i.e.
for (int i = 0; i < list_location.size(); i++) {
createMarker(i, list_location.get(i).getLatitude(),list_location.get(i).getLongitude());
}
and in your createMarker() method
private void createMarker(int index, String latitude, String longitude) {
// Adding the taped point to the ArrayList
if (index == 0)
color = BitmapDescriptorFactory.COLOR_THAT_SUITS;
else if (index == list_location.size()-1)
color = BitmapDescriptorFactory.COLOR_THAT_SUITS;
//set this color to your marker
.....
.....
.icon(BitmapDescriptorFactory.defaultMarker(color)));
}

Delete previous circle before drawing new ones on the map

I have this method:
public void crowdArea(){
for (int n=0; n<place.length; n++) {
Location locationFromDB2 = new Location("");
locationFromDB2.setLatitude(latt2[n]);
locationFromDB2.setLongitude(longg2[n]);
total = 0;
double c [][]=new double[100][2];
double p [][]=new double[100][2];
for (int n2 = 0; n2 < latt.length; n2++) {
Location locationFromDB3 = new Location("");
locationFromDB3.setLatitude(latt[n2]);
locationFromDB3.setLongitude(longg[n2]);
float dist = locationFromDB2.distanceTo(locationFromDB3);
if (dist < 500f) {
c [total][0]=latt[n2];
c [total][1]=longg[n2];
p [total][0]=prelatt[n2];
p [total][1]=prelongg[n2];
total++;
}
}
if (total >= 5) {
draw(latt2[n], longg2[n]);
state[n]="yes";
boolean b=bearing2(c,p);
Log.d("diriction is: ", b+"");
}
else{
state[n]="no";
// draw2(latt2[n], longg2[n]);
}
}
}
public void draw(double lat, double lonng){
Circle myCircle;
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
myCircle = map.addCircle(circleOptions);
}
which draws a circle on my map.
this method is called every 15 secs, but it draws a circle on the previous circle and so on. what I want is to remove previous circles before adding the new one.
how to do this?
You can use map.clear() to clear the map before drawing the new circle.
Alternatively, if you have drawn other objects on the map and you don't want to delete them, just your previous Circle, you can do:
private Circle myCircle;
// ...
public void draw(double lat, double lonng){
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
if (myCircle != null) {
myCircle.remove();
}
myCircle = map.addCircle(circleOptions);
}
FOLLOWUP based on your code update:
private List<Circle> circles = new ArrayList<>();
public void crowdArea(){
clearCircles();
for (int n=0; n<place.length; n++) {
Location locationFromDB2 = new Location("");
locationFromDB2.setLatitude(latt2[n]);
locationFromDB2.setLongitude(longg2[n]);
total = 0;
double c [][]=new double[100][2];
double p [][]=new double[100][2];
for (int n2 = 0; n2 < latt.length; n2++) {
Location locationFromDB3 = new Location("");
locationFromDB3.setLatitude(latt[n2]);
locationFromDB3.setLongitude(longg[n2]);
float dist = locationFromDB2.distanceTo(locationFromDB3);
if (dist < 500f) {
c [total][0]=latt[n2];
c [total][1]=longg[n2];
p [total][0]=prelatt[n2];
p [total][1]=prelongg[n2];
total++;
}
}
if (total >= 5) {
draw(latt2[n], longg2[n]);
state[n]="yes";
boolean b=bearing2(c,p);
Log.d("diriction is: ", b+"");
}
else{
state[n]="no";
// draw2(latt2[n], longg2[n]);
}
}
}
public void draw(double lat, double lonng){
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
circles.add(map.addCircle(circleOptions));
}
private void clearCircles() {
for (Circle circle : circles) {
circle.remove();
}
circles.clear();
}
I suggest you to use
if(mapCircle!=null){
mapCircle.remove();
}
instead of map.clear() because this will remove every objects on the map

How to smoothly keep moving current location marker in Google Maps v2 android

I have placed a marker for my location. I would like to move my marker smoothly something like the Google maps app. The blue circle moves very smoothly when I keep moving in my car. I would like implement the same for my app. how do I implement this on my app?
As of now my location marker just jumps for different location on very location change and marks the marker there.
Here is what I have don:
googleMap.setMyLocationEnabled(true);
So onMyLocationChange method is called automatically:
#Override
public void onMyLocationChange(Location location)
{
curlat = location.getLatitude();
curlong = location.getLongitude();
if (markermylocation != null)
{
markermylocation.remove();
}
curlat = location.getLatitude();
curlong = location.getLongitude();
myLocation(curlat, curlong, username, imageURL, ZOOMVALUE);
}
in mylocaiton method:
private void myLocation(double lat, double lng, String name, String url, float zoom)
{
if(firsttime == 1)
{
LatLng ll = new LatLng(lat,lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
googleMap.animateCamera(update);
firsttime = 0;
}
final String uname = name;
curlat = lat;
curlong = lng;
LatLng position = new LatLng(curlat, curlong);
markerOptionsmylocaiton = new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromBitmap(icon)).title(uname).anchor(0.5f, 1f);
markermylocation = googleMap.addMarker(markerOptionsmylocaiton);
LatLng latlang = new LatLng(curlat, curlong);
animateMarker(markermylocation, latlang, false);
}
So everytime mylocation is called the marker gets removed and calls mylocation method and then creates the marker in the new postition. Instead I would like to get a smooth transition of the marker like Google maps? How to implement this?
Update:
After working couple of times on this: I came to this code finally but then my markers are not showing up:
I am using the below method and calling this every time in myLocation method.
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker)
{
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = googleMap.getProjection();
Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 500;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable()
{
#Override
public void run()
{
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0)
{
// Post again 16ms later.
handler.postDelayed(this, 16);
}
else
{
if (hideMarker)
{
marker.setVisible(false);
}
else
{
marker.setVisible(true);
}
}
}
});
}
Thanks!
Hi I have also the same thing in one of my project and it works like charm.
Add the below piece of code after this line :
marker.setPosition(new LatLng(lat, lng));
//this code
if (googleMap != null)
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15.0f));
Hope it will help you.
Thanks
You'll have to use an interpolator a linear or maybe an acceleration interpolator between the new latlng of the location and the current latlng of the marker. Here's an example with a bounce interpolator.
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 2500;
final Interpolator interpolator = new BounceInterpolator();
marker.setVisible(true);
handler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = Math.max(
1 - interpolator.getInterpolation((float) elapsed
/ duration), 0);
marker.setAnchor(0.5f, 1.0f + 6 * t);
marker.setPosition(latlng)
if (t > 0.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});

Move an icon on google maps

I've searched for similar questions here but none of them helped me.
I have an application running on the map that shows the path taken by the user and an icon in the form of "police". What I want now is that this icon to move the map behind me. So I created a method (stalk) that calculates a new point given the current position of the user, the position of the icon and the map. The method works, but the problem is that when I run my application icon only moves once, because his position is not updated.
Here's part of my code:
public void onMyLocationChange(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
lat = String.valueOf(latitude);
longi = String.valueOf(longitude);
posCurrent = new LatLng(latitude, longitude);
posAtuais.add(posCurrent);
posInicial = posAtuais.get(0);
Marker marker = map.addMarker(new MarkerOptions().position(posInicial));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(posCurrent, 19));
PolylineOptions options = new PolylineOptions().width(5).color(mudaLinhaCor(heart_rate)).geodesic(true);
for (int z = 0; z < posAtuais.size(); z++) {
LatLng point = posAtuais.get(z);
options.add(point);
}
line = map.addPolyline(options);
LatLng posPolice = stalk(posCurrent, POLICE, map);
Marker init = map.addMarker(new MarkerOptions().position(posPolice)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.police)));
}
//A method that computes a new position
public LatLng stalk(LatLng player,LatLng police,GoogleMap mapView){
Projection projection = mapView.getProjection();
Point pointInicial = new Point(); //police
pointInicial = projection.toScreenLocation(police);
Point pointFinal = new Point(); //player
pointFinal = projection.toScreenLocation(player);
double y=0.2;
int x=0;
if((pointInicial.x==pointFinal.x)){
y=pointInicial.y+1;
}else{
double m=(pointFinal.y-pointInicial.y)/(pointFinal.x-pointInicial.x);
double b=pointInicial.y-(m*pointInicial.x);
int i=1;
while(y != (int)y){
if(pointInicial.x<pointFinal.x){
x=pointInicial.x+i;
//System.out.println("entrou no x<xfnal: "+x);
}
else if(pointInicial.x>pointFinal.x){
//System.out.println("entrou no x>xfnal");
x=pointInicial.x-i;
}
y=m*x+b;
//System.out.println("y: : "+y);
i++;
}
}
return projection.fromScreenLocation(new Point(x, (int) y));
}
Someone could help me.
Implement OnMarkerDragListener in your activity then
#Override
public void onMarkerDragStart(Marker arg0) {
markerOptions.position(latLng);
}
#Override
public void onMarkerDragEnd(Marker arg0) {
myMap.clear();
latLng = arg0.getPosition();
markerOptions.position(latLng);
Marker marker = myMap.addMarker(markerOptions);
}

Categories

Resources