I don't know why this polyline won't show, I already put the google maps api and enabled the directions api as well
PolylineOptions rectOptions = new PolylineOptions();
rectOptions.add(new LatLng(24.009115, -104.699933));
rectOptions.add(new LatLng(24.009115, -104.699933));
rectOptions.width(5);
rectOptions.color(Color.RED);
Polyline polyline = mMap.addPolyline(rectOptions);
Add(), width() and color() all return a PolylineOptions with the new settings added.
PolylineOptions rectOptions = new PolylineOptions();
rectOptions =rectOptions.add(new LatLng(24.009115, -104.699933));
rectOptions =rectOptions.add(new LatLng(24.009115, -104.699933));
rectOptions =rectOptions.width(5);
rectOptions =rectOptions.color(Color.RED);
Polyline polyline = mMap.addPolyline(rectOptions);
or
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(24.009115,-104.699933),
new latlang(24.009115,-104.699933))
.width(5)
.color(Color.red);
Polyline polyline = mMap.addPolyline(rectOptions);
Source:https://developers.google.com/android/reference/com/google/android/gms/maps/model/PolylineOptions
Related
I draw ploy line by polylineOptions.addAll() . but i can't remove it from map.how to remove it?
PolylineOptions lineOptions = null;
lineOptions = new PolylineOptions();
lineOptions.addAll(points);
lineOptions.width(5);
lineOptions.color(Color.RED);
You just create object Polyline and just remove it, i think it will be working
PolylineOptions lineOptions = null;
lineOptions = new PolylineOptions();
lineOptions.addAll(points);
lineOptions.width(5);
lineOptions.color(Color.RED);
Polyline polyline = mGoogleMap.addPolyline(lineOptions);
polyline.remove();
or clear google map
I have about 200 Polylines on my Map.
Now i try to find out witch Polyline the User have Click.
But polyline.getId() give me every Time a new random number like PL65 next start of the App PL144 and so on.
Is there any Way to know witch Polyline the user have click ?
I must show for every Polyline a Text with rules.
PolylineOptions spss7 = new PolylineOptions()
.add(new LatLng(52.260803, 8.16152))
.add(new LatLng(52.259113, 8.162186))
.add(new LatLng(52.258438, 8.158634))
.color(Color.GREEN)
.geodesic(true);
Polyline psps7 = googleMap.addPolyline(spss7);
psps7.setClickable(true);
PolylineOptions spss8 = new PolylineOptions()
.add(new LatLng(52.3524987, 7.709607499999999))
.add(new LatLng(52.3524921, 7.7098328))
.add(new LatLng(52.3534915, 7.710031300000001))
.color(Color.GREEN)
.geodesic(true);
Polyline psps8 = googleMap.addPolyline(spss8);
psps8.setClickable(true);
}
googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener(){
public void onPolylineClick(Polyline polyline) {
int strokeColor = polyline.getColor() ^ 0x0000CC00;
polyline.setColor(strokeColor);
Toast.makeText(getActivity(), "Polyline klick: "+polyline.getId(), Toast.LENGTH_LONG).show();
}
You can use the polyline tag to identify the polyline.
PolylineOptions spss7 = new PolylineOptions()
.add(new LatLng(52.260803, 8.16152))
.add(new LatLng(52.259113, 8.162186))
.add(new LatLng(52.258438, 8.158634))
.color(Color.GREEN)
.geodesic(true);
Polyline psps7 = googleMap.addPolyline(spss7);
psps7.setClickable(true);
psps7.setTag(new String("psps7"));
PolylineOptions spss8 = new PolylineOptions()
.add(new LatLng(52.3524987, 7.709607499999999))
.add(new LatLng(52.3524921, 7.7098328))
.add(new LatLng(52.3534915, 7.710031300000001))
.color(Color.GREEN)
.geodesic(true);
Polyline psps8 = googleMap.addPolyline(spss8);
psps8.setClickable(true);
psps8.setTag(new String("psps8"));
}
googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener(){
public void onPolylineClick(Polyline polyline) {
int strokeColor = polyline.getColor() ^ 0x0000CC00;
polyline.setColor(strokeColor);
Toast.makeText(getActivity(), "Polyline klick: " +
(String)polyline.getTag(), Toast.LENGTH_LONG).show();
}
We set the tag for the 2 polylines in this example then in the onClickListener we get the tag and cast it back to a string.
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.
I'm new to the google maps v2, I need to draw a rectangle from point1 to point2, with a custom color and width, I tried to use a Polyline, but all the labels (city name, country name ..) are still visible, so how can I do this ?
thank you
this is what I tried
mMapView.addPolyline(new PolylineOptions()
.add(new LatLng(xx,xx), new LatLng(xx,xx))
.width(50)
.color(Color.parseColor("#f4f3f0")));
Just read the Documentation
Example
GoogleMap map;
// ... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
Use
public class My_Map extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
// Polylines are useful for marking paths and routes on the map.
map.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(-33.866, 151.195))
.add(new LatLng(-18.142, 178.431))
.add(new LatLng(21.291, -157.821))
.add(new LatLng(37.423, -122.091))
);
}
}
For more details just check How to draw free hand polygon in Google map V2 in Android?
I have a list full of LatLng points to create a polygon in google maps android v2, but I cannot figure out how to add this list to the .add area correctly. Here is my list full of points:
list.add(new LatLng(la,lo));
Here is the polygon via Google Dev
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(*******HOW TO ITTERATE LIST************).strokeColor(Color.RED)
.fillColor(Color.BLUE));
How do you go about adding the points?
PolygonOptions opts=new PolygonOptions();
for (LatLng location : list) {
opts.add(location);
}
Polygon polygon = map.addPolygon(opts.strokeColor(Color.RED).fillColor(Color.BLUE));
This assumes that list is something like an ArrayList<LatLng>.
I had the same issue and I find this without need for iteration.
PolygonOptions polOpt =
new PolygonOptions().addAll(latlngs).strokeColor(Color.RED).fillColor(Color.BLUE);
Polygon polygon = mMap.addPolygon(polOpt);
It may help someone. I did the same its working.
ArrayList<LatLng> locations = new ArrayList();
Adding the latLng value in list.
locations .add(new LatLng(2, 2));
locations .add(new LatLng(4, 2));
locations .add(new LatLng(4, 4));
locations .add(new LatLng(2, 4));
locations .add(new LatLng(2, 2))
Creating the GeoFence on the map
PolylineOptions options = new PolylineOptions().width(5).color(Color.RED).geodesic(true);
for (int z = 0; z < locations.size(); z++) {
LatLng point = locations.get(z);
options.add(point);
}
mMap.addPolyline(options);
CameraPosition cameraPosition;
cameraPosition = new CameraPosition.Builder().target(new LatLng(50.8404969, -0.1504184)).zoom(15).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));