Polyline Colour doesn't change - java

The polyline colour doesn't change even after doing this:
Polyline p;
lineOptions=new PolylineOptions();
lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.geodesic(true);
lineOptions.clickable(true);
lineOptions.color(Color.BLACK);
p = mMap.addPolyline(lineOptions);
p.setColor(Color.BLUE);
My question is why?
Points is a previously declared LatLng ArrayList.

If you want to change the color of polyline which is already drawn on the map,then you need to add that polyline (of updated color) to the map, to notify that we have changed the color as below:
lineOptions.color(Color.BLACK);
p = mMap.addPolyline(lineOptions);
if(p !=null) {
p.setColor(Color.BLUE);
mMap.addPolyline(p);
}
It will change the color from black to blue
Hope this helps!

Related

How to get name of polygon or polyline if my location inthere ? (OSMDroid)

Im working with OSMDroid to make map offline in Android Studio.
this is my code to create Polygon :
polygon = new Polygon();
polygon.setPoints(list_polygon);
polygon.setFillColor(Color.parseColor("#1D566E"));
polygon.setTitle(name_map);
polygon.getOutlinePaint().setColor(polygon.getFillPaint().getColor());
map.getOverlays().add(polygon);
this code is to create line :
line = new Polyline();
line.setPoints(list_line);
line.setGeodesic(true);
line.setColor(Color.parseColor("#E33E5A"));
line.getOutlinePaint().setStrokeWidth(30f);
line.setWidth(15f);
line.getPaint().setStrokeCap(Paint.Cap.ROUND);
map.getOverlays().add(line);
and this code is to get my location :
myLocation = new MyLocationNewOverlay(map);
myLocation.enableFollowLocation();
myLocation.setDirectionArrow(icTruk, icTruk);
myLocation.enableMyLocation();
myLocation.setDrawAccuracyEnabled(true);
map.getOverlays().add(myLocation);
I have done to create polygon and polyline in osmdroid.
But now i want read that polygon or polyline if mylocation inthere ?
How to make it posible ?
You could do the following to get the current location and then check if it is close to the Polyline.
MyLocationNewOverlay myLocation= new MyLocationNewOverlay(mapView) {
#Override
public void onLocationChanged(Location location, IMyLocationProvider source) {
super.onLocationChanger(location, source);
// Turn the current location into a GeoPoint
GeoPoint currentPoint = new GeoPoint(location.getLatitude(), location.getLongitude);
// Set tolerance for isCloseTo
// Might need to play around with this value
// and see which fits best for your needs
double tolerance = 5.0;
// Check if location is close to Polyline
if (line.isCloseTo(currentPoint, tolerance, map)) {
// Do here what you want to do,
// when location is close to Polyline
}
}
}
Checking, if the location/GeoPoint is within a given polygon is a bit more work, since the only integrated method for that is based on MotionEvent rather than GeoPoint and only returns a correct value in a very specific scenario, see this for reference. But there are some answers out there, which might be usable for your need, like this one.
References:
Get the current location
OSMDroid Geopoint class
Android Location class
OSMDroid Polyline.isCloseTo
OSMDroid Polygon.contains
One way to check if point is in a polygon

Java Graphics 2d avoid Polyline distorted corners

i'm working on a graphic interface for drawing subway maps. A line is represented with station as circles and a polyline to link them.You can move the stations with a mouseDrag and of course it updates the displaying map in real time. My problem is when stations comes to a certain angle, there is a polyline distortion and the corner created by the 2 lines is out of the station circle display, i'd like to know if there is a way to avoid this.
screenshots of the app with the polyline issue
here's my code for the polyline's draw
//x and y point array creation
xPoints = new int[this.stationViews.size()];
yPoints = new int[this.stationViews.size()];
for (int i=0;i<this.stationViews.size();i++) {
//fill arrays with the center point of circles representing stations
xPoints[i] = this.stationViews.get(i).getStation().getPosX()-this.stationViews.size()/2;
yPoints[i] = this.stationViews.get(i).getStation().getPosY()-this.stationViews.size();
}
//setting color
g2D.setColor(this.line.getColor());
//set stroke width relative to the zoom level
int strokeWidth=5;
if(!this.stationViews.isEmpty()) {
if (this.stationViews.get(0).getStationSize()>14) {
strokeWidth = this.stationViews.get(0).getStationSize()-13;
}else {
strokeWidth = 3;
}
}
g2D.setStroke(new BasicStroke(strokeWidth));
//draw the polyline
if (this.stationViews.size() >1) {
g2D.drawPolyline(xPoints, yPoints, this.stationViews.size());
}
//draw the station (g2D.drawCircle)
for (StationView stationView : stationViews) {
stationView.affiche(g2D,this.line.getColor());
}
thank you for your help
That is called the miter. You seem to be per default using JOIN_MITER, sharp joining of extended lines at the end, which can point far out of the join for small angles.
g2d.setStroke(new BasicStroke(strokeWidth,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 5));
miter a surface forming the beveled end or edge of a piece where a joint is made by cutting two pieces at an angle and fitting them together.
It is also a bishop's cap with a pointy top, hence the name.

Drawing Circle on Map around current location and showing only markers from that circle rather than outside circle in android

I was able to draw circle on map around my current location but I want to show the markers only inside that circle and hide other markers from map.
You need to first draw a circle and then compare each marker either it is inside or outside circle, by using this method.
private void checkCircle(){
float[] distance = new float[2];
Location.distanceBetween(marker_latitude, marker_longitude,
circle.getCenter().latitude, circle.getCenter().longitude, distance);
if(distance[0] > circle.getRadius() ){
Toast.makeText(getContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Inside", Toast.LENGTH_LONG).show();
}
}
If you are getting that your marker is in outside than dont need to add in map.

Polylines not being removed from GoogleMap

I have a Map where i'm placing markers and connecting them with PolyLines:
PolylineOptions p = new PolylineOptions();
p.color(Color.BLUE);
p.width((float) 7.0);
Polyline polyLine = this.mMap.addPolyline(p);
p.add(actualLocation);
LatLng previousPoint = latLngs.get(latLngs.size() - 2);
p.add(previousPoint);
this.polyLines.add(polyLine);
mMap.addPolyline(p);
I'm saving the object p in an arraylist:
ArrayList<Polyline> polyLines = new ArrayList<>();
When i remove the last marker i want to remove the last polyline too. I'm doing the next:
if (polyLines.size() > 0) {
Polyline polyLine = polyLines.get(polyLines.size() - 1);
polyLine.remove();
polyLines.remove(polyLines.size() - 1);
}
I'm removing the marker but the polyline keeps in the Map. I'm removing it from the ArrayList too.
Can anyone help me to find out what is happening? I've tried to make the polyline invisible or changing the colour but it won't work.
It looks to me like you are in fact adding two polylines to the map... If PolylineOptions() are mutable once attached to a Polyline, they are in the same spot, otherwise you are adding one without positioning followed by a second. Only the second Polyline is being added to your List.
Instead of this:
PolylineOptions p = new PolylineOptions();
p.color(Color.BLUE);
p.width((float) 7.0);
Polyline polyLine = this.mMap.addPolyline(p); // Add before location set
p.add(actualLocation);
LatLng previousPoint = latLngs.get(latLngs.size() - 2);
p.add(previousPoint);
this.polyLines.add(polyLine);
mMap.addPolyline(p); // Add after location set
Did you want to do this?
PolylineOptions p = new PolylineOptions();
p.color(Color.BLUE);
p.width((float) 7.0);
p.add(actualLocation);
LatLng previousPoint = latLngs.get(latLngs.size() - 2);
p.add(previousPoint);
Polyline polyLine = mMap.addPolyline(p);
this.polyLines.add(polyLine);

How to draw a circle covering particular distance on map

I am using GoogleMap fragment to show some location data using markers. What I want to do is, draw a circle with a radius of specific distance on map, so that the circle will cover certain area approximately on map. I am not sure where to start , I am already having a map displaying location data, from location data I can figure out at what distance those points are from centre location.
Any help would be great.
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(37.4, -122.1))
.radius(1000)); // In meters
// Get back the mutable Circle
Circle circle = myMap.addCircle(circleOptions);
For detailed info check : https://developers.google.com/maps/documentation/android-api/shapes
Use this function where you need to pass LatLng as paramater when you call it.
private void drawCircle(LatLng point){
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(20);
// Border color of the circle
circleOptions.strokeColor(Color.BLACK);
// Fill color of the circle
circleOptions.fillColor(0x30ff0000);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
googleMap.addCircle(circleOptions);
}
Use GoogleMap Circle API. You just need to pass the Required inputs and it's done. Follow link from example. e.g from docs , a simple code structure will look like this
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(-33.87365, 151.20689)) // center point of map
.radius(10000) // radius of circle to cover on map
.strokeColor(Color.RED) //color of circle boundary
.fillColor(Color.BLUE)); //color of circle to fill ,
// make sure choose the light color close to transparent

Categories