Draw Polylines and Polygons on Map in android studio? - android

Actually i am beginner to android development
I want to draw route(blue line) between my current location to marker i touch(i have some location markers on map,locations of clients)??

A Polyline is a series of connected line segments. Polylines are useful to represent routes, paths, or other connections between locations on the map.
Create a PolylineOptions object and add points to it. Each point represents a location on the map, which you define with a LatLng object containing latitude and longitude values. The code sample below creates a polyline with 6 points.
Call GoogleMap.addPolyline() to add the polyline to the map.
Polyline polyline = googleMap.addPolyline(new PolylineOptions()
.clickable(true)
.add(
new LatLng(-35.016, 143.321),
new LatLng(-34.747, 145.592),
new LatLng(-34.364, 147.891),
new LatLng(-33.501, 150.217),
new LatLng(-32.306, 149.248),
new LatLng(-32.491, 147.309)));
Set the polyline's clickable option to true if you want to handle click events on the polyline.
You can customise stroke width, stroke color, and joint type:
polyline.setEndCap(new RoundCap());
polyline.setWidth(12);
polyline.setColor(Color.parse("#0000ff"));
polyline.setJointType(JointType.ROUND);

Related

How to retrieve latitude and longitude of Google Maps android

How can I retrieve latitude and longitude of Google Maps data from my database and display it to the user on the map in android ?
I have a database mysql containing some fields as longitude and latitude.
What is the best way to do such?
Is it possible through a Volley library?
If there is an example of this, it is good to me
Yes you can draw stuff in the onMapReady callback. You can create Markers, draw circles, draw polygons.
To add a marker just call:
LatLng point = new LatLng(exampleLat, exampleLng);
mGoogleMap.addMarker(new MarkerOptions().position(point));
Here is an example of how to draw a circle:
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lng))
.radius(radius)
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT);
Circle circle = mGoogleMap.addCircle(circleOptions);
Here is a Polygon:
PolygonOptions polygonalOptions = new PolygonOptions()
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT)
.clickable(true);
for (List<Double> latLng : myPolygon.path) {
LatLng point = new LatLng(latLng.get(0), latLng.get(1));
polygonalOptions.add(point);
}
mGoogleMap.addPolygon(polygonalOptions);
PS: Writing from my phone sorry for any mistakes or formatting issues.

Dynamically draw a single polyline to current location

In my android app, I want to create a single path/polyline from a specific location(far end) to my current location while moving. I could draw the polyline on map with now issue while when I moves to a new location another polyline is been drown on the map, so my map output is full of polylines whichh I don't need, I want only one polyline to be visible to my current location.
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
Polyline polyline = googleMap.addPolyline(polylineOptions);
Remove old Line before adding new one.
Polyline polyline ;
public void addUpdatePolyLine()
{
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
if(polyline !=null)
{
polyline.remove();
}
polyline = googleMap.addPolyline(polylineOptions);
}

Make clickable polygons on Google Maps (for Android)

I have continuous LatLngs of various areas in a city. Is there any way I can create clickable polygons with it. Once way to go about would be to
Generate polygons with the available LatLngs.( I want to visually show the polygons on the map with color encoding)
Set up setOnMapClickListener.
Do a point inside polygon test.
I understand that this is very naive. What are the alternative approaches?
Here's how I did it.
Polygon polygon = getMap().addPolygon(new PolygonOptions()
.add(new LatLng(12.780712, 77.770956), new LatLng(12.912006, 77.229738), new LatLng(12.412006, 77.629738), new LatLng(12.912006, 77.229738))
.strokeColor(0xFF00AA00)
.fillColor(0x2200FFFF)
.strokeWidth(2)
);
polygon.setClickable(true);
getMap().setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
public void onPolygonClick(Polygon polygon) {
mClusterManager = new ClusterManager<MyItem>(getApplicationContext(), getMap());
getMap().setOnCameraChangeListener(mClusterManager);
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(getMap().getCameraPosition().target, getMap().getCameraPosition().zoom));
try {
readItems();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
}
});
Hope that helps.
you don't need to go crazy for having clickable polygon. I did it time ago but now, there is an api for that:
GoogleMap.setOnPolygonClickListener(OnPolygonClickListener)
You can use it easily:
GoogleMap mymap =....//init your map
mymap.setOnPolygonClickListener(new OnPolygonClickListener(){
void onPolygonClick(Polygon polygon){
//do whatever with polygon!
}
});
In adding Polygon to the map. First create a PolygonOptions object and add some points to it. These points will form the outline of the polygon. You then add the polygon to the map by calling GoogleMap.addPolygon(PolygonOptions) which will return a Polygon object.
This following code snippet show how to add polygon to a map.
// Instantiates a new Polygon object and adds points to define a rectangle PolygonOptions rectOptions = new PolygonOptions()
.add(new LatLng(37.35, -122.0),
new LatLng(37.45, -122.0),
new LatLng(37.45, -122.2),
new LatLng(37.35, -122.2),
new LatLng(37.35, -122.0));
// Get back the mutable Polygon Polygon polygon = myMap.addPolygon(rectOptions);
By default, polygons are not clickable. You can enable and disable the clickability by calling Polygon.setClickable(boolean).
Like N Dorigatti said. In using OnPolygonClickListener to listen click events, call GoogleMap.setOnPolygonClickListener(OnPolygonClickListener).
When a user clicks on a polygon, you will receive an onPolygonClick(Polygon) callback.
Check this document for more information.

Remove a line from PolyLine map in Google Map android V2?

I have searched in google and find this answer where i can remove all polyLine of a Map.
But I want to remove only a particular line from the poly Line. For Example I want to remove the line from 2nd to 3rd LatLng in the given code. I want to change color of a particular line of a polyLine or make it transparent. And I also want to add a clickListener to PolyLine
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)).width(5).color(Color.RED);; // Closes the polyline.
Polyline polyline = myMap.addPolyline(rectOptions);
Main goal is to remove/make it transparent a particular line of a PolyLine on click or tap.
PolylineOptions line= new PolylineOptions().add(HAMBURG,// these are latlong
KIEL,
KIEL2,
KIEL3
new LatLng(40.748963847316034,
-73.96807193756104)
)
.width(5).color(Color.RED);
Polyline polyline= googleMap.addPolyline(line);
And i want to remov line between KIEL1 and KIEL2
You'll have to manually remove points from the polyline.
EDIT:
Step by step:
Create a List of polylines:
List<Polyline> mPolylines = new ArrayList<Polyline>();
Add PolylineOptions to the map:
Polyline polyline1 = myMap.addPolyline(rectOptions1);
Polyline polyline2 = myMap.addPolyline(rectOptions2);
Polyline polyline3 = myMap.addPolyline(rectOptions3);
Then save the added polylines to your array
mPolylines.add(polyline1);
mPolylines.add(polyline2);
mPolylines.add(polyline3);
Now at any time you can trim the polyline like so:
// Get polyline1
List<LatLng> points = mPolylines.get(0).getPoints();
// Set the bounds of points to remove (inclusive)
int startPoint = 1, endPoint = 2; // will remove kiel1 and kiel2
// Remove the points
for (int i=startPoint; i<=endPoint; i++) {
points.remove(i);
}
// Added this line as getPoints returns a copy
mPolylines.get(0).setPoints(points);
Now in theory this should work fine. I found that the points don't actually change after setPoints.
I even tried:
Polyline polyline = mPolylines.get(0);
// Get copy of the points
List<LatLng> points = polyline.getPoints();
mPolylines.get(0).remove();
mPolylines.remove(0);
for (int i=3000; i<7000; i++) {
points.remove(i);
}
// Create a PolylineOptions object with the new points
PolylineOptions polylineOptions = new PolylineOptions().addAll(points);
mPolylines.add(0, mMap.addPolyline(polylineOptions));
And to my surprise a new Polyline was added (I can tell by the changed stroke width and color), but it still used the old points, even though points.size() returned the correct (trimmed) count.
I'm not sure why this is so, perhaps some error in my code. You can try these methods yourself and see if you are any luckier.

How to remove all the polylines from a map

Following
How to draw a path between two markers
I had to add lot of polylines between two markers, to make a path.
One of the markers is draggable, lets say source is draggable.
So, when user starts dragging the marker, the path previously drawn must be erased and a new path between new source and destination must be draw.
I am able to draw the new path, but how can i erase the previous path?
This is how the path is drawn:
for (int z = 0; z < list.size() - 1; z++) {
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
}
One solution i can get is
map.clear();
To clear all the polylines, markers etc.. and add the markers again, then drawn the path.
But as soon as I start dragging, the marker is cleared, hence not visible on the map :(
Thank You
Keep track of the Polyline as you add it to the map:
Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);
Then when you want to remove it:
polyline.remove();
If you have lots of Polylines, just add them to a List as they are put on the map:
List<Polyline> polylines = new ArrayList<Polyline>();
for(....)
{
polylines.add(this.mMap.addPolyline(new PolylineOptions()....));
}
And when you want to delete:
for(Polyline line : polylines)
{
line.remove();
}
polylines.clear();
The key is to keep a reference to the Polyline objects and call .remove() on each one.
I know this is very old question but I noticed that this is very common need. I found another way and I wanted to share it.
Here is the basic idea:
Polyline polylineFinal;
PolylineOptions polylineOptions;
loop {
polylineOptions.add( new LatLng( latitude, longitude ) );
}
polylineOptions.width(2);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);
polylineFinal = map.addPolyline (polylineOptions);
Map's "addPolyline" method returns a single polyline which contains all the points. When I need to remove the points, I call polylineFinal's "remove" method.
polylineFinal.remove();
Update
If you want to removes all markers, polylines, polygons, overlays, etc from the map use
mMap.clear(); //it will remove all additional objects from map
and if you only want to remove all or single polyline, polygon, marker etc see #DiskDev Answer above. In this case you must keep track of every single additional object you add to map

Categories

Resources