Drawing Polygon on Map - java

I'am trying to draw a polygon with about 260 coordinates on my Map. But I cant figure out why the polygon is not drawn ? There are no null references in the PolygonOptions nor are their wrong data.
protected void drawOverlayPolygon(JSONArray coordinates) {
PolygonOptions rectOptions = new PolygonOptions();
if (coordinates != null) {
int j = 0;
while(!coordinates.isNull(j))
j++;
ArrayList<LatLng> points = new ArrayList<LatLng>(j);
for (int i = 0; i < j; i++) {
try {
JSONArray latlng = coordinates.getJSONArray(i);
// latlng = coordinates.getJSONArray(i);
if (latlng != null) {
LatLng point = new LatLng(Float.parseFloat(latlng
.getString(0)), Float.parseFloat(latlng
.getString(1)));
// rectOptions.add(point);
points.add(point);
Log.i("LatLng Polygon",
Float.parseFloat(latlng.getString(0)) + " / "
+ Float.parseFloat(latlng.getString(1)));
}
} catch (JSONException e) {
e.printStackTrace();
}
// }
}
rectOptions.addAll(points);
rectOptions.strokeColor(Color.RED);
rectOptions.fillColor(Color.BLUE);
Polygon polygon = mapFragment.getMap().addPolygon(rectOptions);
polygon.setVisible(true);
Polygon polygon2 = mapFragment.getMap().addPolygon(
new PolygonOptions()
.add(new LatLng(0, 0), new LatLng(0, 5),
new LatLng(3, 5)).strokeColor(Color.RED)
.fillColor(Color.BLUE));
}
}
For testing I added a simple Polygon (polygon2) and it gets drawn. I am parsing the coordinates from a JSON File. I logged the corrds and they seem to be alright. One thing I noticed is that die JSONArray coordinates contains null references.
Can anybody give me a clue ?

try adding:
rectOptions.visible(true);
before adding the Polygon to the map.

Thx Steffi I tried to add that but did not work. Everyone who runs in this problem check if the order of your Lat Lng Coordinates is correct. Otherwise your polygone is drawn somewhere in the nowhere :)

Related

Java android google maps change color polyline where I was

This is how I draw a polyline :
List<LatLng> latLngsList = new ArrayList<>();
for (LegsItem leg : response.body().getRoutes().get(0).getLegs()) {
for (StepsItem step : leg.getSteps()) {
List<LatLng> latLngs = PolyUtil.decode(step.getGeometry());
step.setLatLngs(latLngs);
latLngsList.addAll(latLngs);
}
}
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.addAll(latLngsList));
I draw this on color black but when I am on the polyline(In LatLtg), I want to change the color to blue. To detect if I am on the point, I use the below:
mMap.setOnMyLocationChangeListener
and check if the first not done point is near than 2 m :
double dis = currentLocation.distanceTo(location);
But it does not work correctly
You can use the function distanceBetween which Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.
public static boolean checkDistance(LatLng oldPosition, LatLng newPosition) {
float[] results = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
newPosition.latitude, newPosition.longitude, results);
return results[0] <= 50.0;
}
Here i set minimum distance 50 meter. these will return true if your current location(latlon) is within 50 miters from your polyline latlon.
Define color value in colors.xml
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#0000EE</color>
</resources>
and set color like this :-
Polyline line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.color(R.color.blue));
EDIT
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
for (LatLng nLatLng : latLngsList) {
double latitude = nLatLng.latitude;
double longitude = nLatLng.longitude;
double myLat = location.getLatitude();
double myLong = location.getLongitude();
if (latitude == myLat && longitude == myLong) {
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.add(nLatLng)
.color(R.color.blue));
} /*else {
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.add(nLatLng)
.color(R.color.black));
}*/
}
}
});
See also: update-polyline-according-to-the-user-moving-android-googlemaps

Processing unfolding maps for each marker

I want to add an image for each marker i have on unfolding maps
Already tried to use Buffer but im not sure what i should do now.
My main class
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.marker.*;
UnfoldingMap map;
import java.util.Map; // import java hashmaps
// declare country hashmaps
// key // value
HashMap<String, Country> countries = new HashMap<String, Country>();
//PGraphics buffer;
int countryNumber;
Button country;
public void setup() {
size(800, 600, P2D);
smooth();
//buffer = createGraphics(800, 600);
map = new UnfoldingMap(this);
map.setTweening(true);
map.zoomToLevel(3);
MapUtils.createDefaultEventDispatcher(this, map);
String[] lines = loadStrings("test.csv"); //read file and save lines in a string array
countryNumber = lines.length; // how many lines
println(countryNumber);
for (int i=0; i<countryNumber; i++) { //all csv lines
String line = lines[i]; /// get data from line. Ex: "Portugal,10,91"
String[] elements = split(line, ","); //separate line by ','
String name = elements[0]; // get country name
float lat = parseInt(elements[20]); // lat
float lon = parseInt(elements[21]); // lon
PImage flagIcon;
flagIcon = loadImage(elements[22]); //get image flag name
Country P = new Country(name, lat, lon, flagIcon);
Location mapMarker = new Location(lat,lon);
// Create point markers for locations
SimplePointMarker eachMapMarker = new
SimplePointMarker(mapMarker);
// Add markers to the map
map.addMarkers(eachMapMarker);
paises.put(nome, P );
}
}
public void draw() {
background(255);
map.draw(); // draw map
//image(buffer, 200, 50);
for (Map.Entry p : Countries.entrySet() ) { // get countries hashmap
Country country = (Country) p.getValue();
country.drawInfo();
country.drawCoor();
country.drawIcon();
}
}
My Country class
//my country class
class Country {
String name;
float lat;
float lon;
int posX, posY;
PImage flagIcon;
Pais (String n, float la, float lo, PImage ic) {
name = n;
lat = la;
lon = lo;
flagIcon = ic;
}
void drawIcon() {
image(flagIcon,lat,lon,16,16);
}
void drawInfo() {
Location mapLocal = new Location(lat, lon);
ScreenPosition mapPos = map.getScreenPosition(mapLocal);
float scale = map.getZoom();
// if pointer is near a country
boolean onOver = false;
if (dist(mouseX, mouseY, mapPos.x, mapPos.y)<2*scale) {
onOver = true;
}
if (onOver) {
noStroke();
//fill(255,255,255,255);
//rect(mouseX+18, mouseY-20, 120, 50);
fill(0);
text(name + "\nLat: " + lat + "\nLon: " + lon , mouseX+25, mouseY-5);
}
}
void drawCoor() {
//println(coordinates);
fill(0);
text("coordinates: " + mouseX + " " + mouseY, 650,10);
}
}
Image links are stored on my CSV
This is what im getting and as i can see my images arent synced with my lat and lon. Can anyone give me an hand ?
When you draw the country icon on the map, for the input of x and y in the image(PImage image, float x, float y) function inside drawIcon() method, you are supposed to use the marker's position on the map instead of the latitude and longitude properties.
The specific method is called getScreenPosition(UnfoldingMap map) of AbstractMarker. Please refer to the javadoc for the details. http://unfoldingmaps.org/javadoc/de/fhpotsdam/unfolding/marker/AbstractMarker.html#getScreenPosition(de.fhpotsdam.unfolding.UnfoldingMap)
The reason behind it is that (I think) the markers are added to the map with the map.addMarker method, then rendered together with the map.draw() method, where the latitude and longitude are transferred to their screen positions at the backstage.
Because you are trying to add the country icons on top of the country markers after the map.draw() method, you should try to associate the icon's position to the marker's screen position.
public void draw() {
background(200);
map.draw();
for (Marker m: countryMarkers) {
image(countryIcon, (((AbstractMarker) m).getScreenPosition(map).x,
((AbstractMarker) m).getScreenPosition(map).y);
}
}
It would also help the code look more organized and clean if you make a CountryMarker class extends SimplePointMarker class, which is available in the unfolding maps library.

How to use GeometricShapeFactory in geoTools to create a Circle on map

I am currently using the below code to create a GeoJson Polygon. this gives me a bad circle which is not valid...
in this case RADIUS = 1609.34 , which is 1 mile in meters.
public GeoJsonPolygon createRadiusPolygon( Point point,double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new com.vividsolutions.jts.geom.Coordinate(point.getX(), point.getY()));
shapeFactory.setSize(RADIUS * 2);
com.vividsolutions.jts.geom.Geometry circle = shapeFactory.createCircle();
List<Point> points = new ArrayList<Point>();
for (com.vividsolutions.jts.geom.Coordinate coordinate : circle.getCoordinates()) {
Point lngLatAtl = new Point(coordinate.x, coordinate.y);
points.add(lngLatAtl);
}
Collections.reverse(points);
return new GeoJsonPolygon(points);
}
referenced:
http://docs.geotools.org/stable/userguide/library/jts/geometry.html
currently if i use Point(-73.87,40.84) RADIUS = 1609.34, i get the below link.
https://gist.githubusercontent.com/VanitySoft/56c4ce0f5c1c7e7fe0461ed46fd5ed11/raw/94544750a140d81780ebe9206395a21ab88bb1f7/circle
===SOLVED== from #Ian answer:
Using method in his answer. RADIUS is in miles, to get the Circle used to create the GeoJson.
...
com.vividsolutions.jts.geom.Point jtsPoint = new GeometryFactory().createPoint(new com.vividsolutions.jts.geom.Coordinate(point.getY(), point.getX()));
javax.measure.Measure measure = Measure.valueOf(RADIUS, NonSI.MILE);
com.vividsolutions.jts.geom.Geometry circle = createCircleRadis(measure,CRS.decode("epsg:4326"),jtsPoint );
...
...
Your output circle is valid, it just happens to exceed the diameter of the Earth's surface so your GIS may have issues drawing it! The problem is that you are mixing degrees and meters indiscriminately and GeoTools has no clue what you want it to do.
You need to add some information about the coordinate reference system of the point to the program, and if that projection is geographic (i.e. in degrees), transform the problem to a projection that is in meters.
public Geometry bufferPoint(Measure<Double, Length> distance, CoordinateReferenceSystem origCRS, Geometry geom) {
Geometry pGeom = geom;
MathTransform toTransform, fromTransform = null;
// reproject the geometry to a local projection
Unit<Length> unit = distance.getUnit();
if (!(origCRS instanceof ProjectedCRS)) {
double x = geom.getCoordinate().x;
double y = geom.getCoordinate().y;
String code = "AUTO:42001," + x + "," + y;
// System.out.println(code);
CoordinateReferenceSystem auto;
try {
auto = CRS.decode(code);
toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
fromTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);
pGeom = JTS.transform(geom, toTransform);
unit = SI.METER;
} catch (MismatchedDimensionException | TransformException | FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
unit = (Unit<Length>) origCRS.getCoordinateSystem().getAxis(0).getUnit();
}
// buffer
Geometry out = pGeom.buffer(distance.doubleValue(unit));
Geometry retGeom = out;
// reproject the geometry to the original projection
if (!(origCRS instanceof ProjectedCRS)) {
try {
retGeom = JTS.transform(out, fromTransform);
} catch (MismatchedDimensionException | TransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return retGeom;
}
AUTO:42001,x,y is a special projection centred on the point x,y in meters that allows us to use the JTS buffer method which is easier than the circle operation you are using.
For your inputs this gives me an ellipse over New York, note this is expected and is due to the distorting effects of using unprojected Lat/Lon coordinates on a curved Earth.
You can call it using:
//Measure<Double, Length> dist = Measure.valueOf(50.0, SI.KILOMETER);
Measure<Double, Length> dist = Measure.valueOf(1.0, NonSI.MILE);
GeometryFactory gf = new GeometryFactory();
Point p = gf.createPoint(new Coordinate(-73.87,40.84));
buf.bufferPoint(dist, DefaultGeographicCRS.WGS84, p);
double latitude = 40.689234d;
double longitude = -74.044598d;
double diameterInMeters = 2000d; //2km
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(64); // adjustable
shapeFactory.setCentre(new Coordinate(latitude, longitude));
// Length in meters of 1° of latitude = always 111.32 km
shapeFactory.setWidth(diameterInMeters/111320d);
// Length in meters of 1° of longitude = 40075 km * cos( latitude ) / 360
shapeFactory.setHeight(diameterInMeters / (40075000 * Math.cos(Math.toRadians(latitude)) / 360));
Polygon circle = shapeFactory.createEllipse();

Android Google Map change polyline colors

I am drawing polylines on my Google Map. I'm doing it using:
private Map<UUID, PolylineOptions> data;
private void drawFeatures() {
for (Feature feature : features) {
feature.setUuid(UUID.fromString((String) feature.getProperties().get("id")));
PolylineOptions options = new PolylineOptions();
List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates();
for (Coordinates coordinate : coordinates) {
// can't use "addAll(...) since 'coordinates' are not instance of 'LatLng'
options.add(new LatLng(coordinate.getLatitude(), coordinate.getLongitude()));
options.color(Color.RED);
}
mMap.addPolyline(options);
data.put(feature.getUuid(), options);
}
}
And then everything is OK. All my polylines are correctly drawed using the good width and color.
However, after that, I'm trying to update the width and the color (without removing and redrawing all the polylines).
I'm trying to do it with:
private void changeColor() {
for (Map.Entry<UUID, PolylineOptions> entry : data.entrySet()) {
entry.getValue().color(Color.CYAN);
}
}
But there is no changes on my map :/ I've read the Google Developers documentation and I don't find anything about that.
How can I update the color of a polyline without having to remove and re-add it ?
PolylineOptions is just a builder for Polylines which are the objects that are drawn into the map.
Thus, changing the PolylineOptions will not affect the Polylines once they are on the map.
Your private Map<UUID, PolylineOptions> data; should be private Map<UUID, Polyline> data; and you need to add elements to the Map like this:
data.put(feature.getUuid(), mMap.addPolyline(options));
may this help you
MarkerOptions markerOptions = new MarkerOptions();
if (!status.equals("ZERO_RESULTS")) {
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<LatLng>();
points.add(driverLatLng);
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = result.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
points.add(storeLatLng);
if (points.contains(storeLongitude)) {
int index = points.indexOf(storeLongitude);
AppLog.Log(TAG, "Index Value in " + index);
int length = points.size();
points.subList(index, length - 1);
AppLog.Log(TAG, "Arraylist Size after SubList Array" + points.size());
}
lineOptions.addAll(points);
lineOptions.width(13);
lineOptions.color(getResources().getColor(R.color.title_background));
lineOptions.geodesic(true);
lineOptions.zIndex(100);
AppLog.Log(TAG, "lineOptions is" + lineOptions.getPoints());
}
mMap.addPolyline(lineOptions);
//DrawArrowHead(mMap, driverLatLng, storeLatLng);
} else {
GlobalMethod.snackBar(true,activity_driver,appContext,"no root found.");
}

Add polygon on google ampv2 android

I want to add polygon on map.here i am retrieving lat lng values from arraylist.I am trying to add a polygon from these lat lng values .But the polygon not added on map.please tell me where i made a mistake
my code
polarl=(ArrayList<HashMap<String, String>>)
getIntent().getSerializableExtra("polMyList");
Log.e("POL ARRAY", polarl.toString());
Polygon polygon;
LatLng polLatLng;
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
Log.e("POL LAT LANG", ""+polLatLng);
rectOptions = new
PolygonOptions().add(polLatLng).fillColor(Color.BLUE).strokeColor(Color.RED);
Polygon polygon1 = _googleMap.addPolygon(rectOptions);
}
}
}
Try this way
Polygon polygon;
LatLng polLatLng;
PolygonOptions rectOptions=new PolygonOptions();
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
Log.e("POL LAT LANG", ""+polLatLng);
rectOptions.add(polLatLng);
}
}
}
Polygon polygon1 = _googleMap.addPolygon(rectOptions.strokeColor(Color.RED).fillColor(Color.BLUE));
For more information go to this SO POST
Try this code,
ArrayList<LatLng> points= new ArrayList<LatLng>();
polarl=(ArrayList<HashMap<String, String>>)getIntent().getSerializableExtra("polMyList");
LatLng polLatLng;
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
points.add(polLatLng);
}
}
}
polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.TRANSPARENT);
polygonOptions.strokeColor(Color.RED);
polygonOptions.strokeWidth(3);
polylineOptions.color(Color.RED);
polylineOptions.width(3);
polylineOptions.addAll(points);
map.addPolyline(polylineOptions);

Categories

Resources