Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

circle polygon looks less like a circle in newer Mapbox version on Android #13125

Closed

Description

@p-fischer

I am upgrading the Android Mapbox SDK from version 4.2.2 to 6.6.0.
In order to draw circles around a specific coordinate I'm calculating 180 perimeter points and drawing a polygon.

On version 4.2.2 the circle (with a radius of 10 meters) looks good:
circle-mapbox-4-2-2

Whereas in version 6.6.0 it looks like either less points were used or their positions are not accurate:
circle-mapbox-6-6-0

Here is the code I'm equally using in both versions:

  • for drawing the circle:
mapBox.addPolygon( new PolygonOptions()
	.addAll( circleCoordinates( coordinate, radius ) )
	.fillColor( device.getColor() )
	.strokeColor( device.getColor() )
	.alpha( TRACKING_RADIUS_ALPHA ) );
  • for calculating the perimeter points:
/**
 * @param radius in meters
 * @return equally distributed points of the circle (first and last coordinate is equal)
 */
public static List<LatLng> circleCoordinates( LatLng location, double radius )
{
	int degreesBetweenPoints = 2; //180 sides
	int numberOfPoints = ( int ) Math.floor( 360 / degreesBetweenPoints );
	double distRadians = radius / EARTH_RADIUS;
	double centerLatRadians = location.getLatitude() * PI / 180;
	double centerLonRadians = location.getLongitude() * PI / 180;
	List<LatLng> polygons = new ArrayList<>(); //array to hold all the points
	for ( int index = 0; index <= numberOfPoints; index++ )
	{
		double degrees = index * degreesBetweenPoints;
		double degreeRadians = degrees * PI / 180;
		double pointLatRadians = asin( sin( centerLatRadians ) * cos( distRadians ) + cos( centerLatRadians ) * sin( distRadians ) * cos( degreeRadians ) );
		double pointLonRadians = centerLonRadians + atan2( sin( degreeRadians ) * sin( distRadians ) * cos( centerLatRadians ),
				cos( distRadians ) - sin( centerLatRadians ) * sin( pointLatRadians ) );
		double pointLat = pointLatRadians * 180 / PI;
		double pointLon = pointLonRadians * 180 / PI;
		LatLng point = new LatLng( pointLat, pointLon );
		polygons.add( point );
	}
	return polygons;
}

Can you tell me if something has changed along the way or if there is a more idiomatic approach to drawing a filled circle?

Configuration

Android versions: 6.0.1
Device models: Samsung Galaxy S5
Mapbox SDK versions: 6.6.0

Activity

  1. changed the title [-]circle polygon looks less like a circle in newer Mapbox version[/-] [+]circle polygon looks less like a circle in newer Mapbox version on Android[/+] on Oct 18, 2018
  2. added
    Android
    on Nov 5, 2018
  3. langsmith commented on Nov 6, 2018

    @langsmith
    Contributor

    Hey @p-fischer , thanks for using Mapbox. You should use a CircleLayer.

    • Build a Feature object with the coordinates that you want to be the middle of the circle.
      Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(LONG,LAT));. If you want multiple circles, do this for each long/lat pair.
    • Add that Feature to a List<Feature> (or all Feature objects to the same list).
    • Use that list to create a FeatureCollection.
    • Use that FeatureCollection to create a GeoJsonSource. Add the GeoJsonSource to the map.
    • Use that GeoJsonSource to create a CircleLayer.
    • Use data-driven styling to customize the look of the circle(layer).
    • Add the layer to the map.
    FeatureCollection featureCollection = FeatureCollection.fromFeatures(List<Feature>);
    
    GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", featureCollection);
    
    mapboxMap.addSource(geoJsonSource);
    
    CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
    
    circleLayer.withProperties(
      PropertyFactory.circleRadius(CIRCLE_RADIUS),
      PropertyFactory.circleOpacity(TRACKING_RADIUS_ALPHA),
      PropertyFactory.circleColor(device.getColor()),
      PropertyFactory.circleStrokeColor(device.getColor())
    );
    
    mapboxMap.addLayer(circleLayer);

    https://www.mapbox.com/android-docs/maps/examples/style-circles-categorically/

    https://www.mapbox.com/android-docs/maps/overview/runtime-styling/#geojson

    Mapbox Annotation Plugin for Android (cuts down on boilerplate code that I wrote above): https://www.mapbox.com/android-docs/plugins/overview/annotation/#manager

  4. p-fischer commented on Mar 8, 2019

    @p-fischer
    Author

    Hi @langsmith
    I know I'm quite late to the discussion. I haven't switched to the annotation API yet.
    But I'm upgrading to the mapbox-android SDK version 7.2.0 and I saw that the "polygon drawing API" is marked as deprecated now.

    My question for CircleLayer is: Does the API (or the plugin API) provide the possibility to set the radius in both "pixels" and "meters"?
    My original example draws a circle based the latter (by calculating the LatLng coordinates).

    Since my map is zoomable, I need define the circles' radiuses in both ways at different places. Is this possible?

  5. langsmith commented on Mar 12, 2019

    @langsmith
    Contributor

    Work in progress @p-fischer . Discussion happening at #4312 . @tobrun @LukasPaczos , any more up-to-date thoughts about this?

  6. miladsalimiiii commented on Dec 13, 2021

    @miladsalimiiii

    Hey @p-fischer , thanks for using Mapbox. You should use a CircleLayer.

    • Build a Feature object with the coordinates that you want to be the middle of the circle.
      Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(LONG,LAT));. If you want multiple circles, do this for each long/lat pair.
    • Add that Feature to a List<Feature> (or all Feature objects to the same list).
    • Use that list to create a FeatureCollection.
    • Use that FeatureCollection to create a GeoJsonSource. Add the GeoJsonSource to the map.
    • Use that GeoJsonSource to create a CircleLayer.
    • Use data-driven styling to customize the look of the circle(layer).
    • Add the layer to the map.
    FeatureCollection featureCollection = FeatureCollection.fromFeatures(List<Feature>);
    
    GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", featureCollection);
    
    mapboxMap.addSource(geoJsonSource);
    
    CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
    
    circleLayer.withProperties(
      PropertyFactory.circleRadius(CIRCLE_RADIUS),
      PropertyFactory.circleOpacity(TRACKING_RADIUS_ALPHA),
      PropertyFactory.circleColor(device.getColor()),
      PropertyFactory.circleStrokeColor(device.getColor())
    );
    
    mapboxMap.addLayer(circleLayer);

    https://www.mapbox.com/android-docs/maps/examples/style-circles-categorically/

    https://www.mapbox.com/android-docs/maps/overview/runtime-styling/#geojson

    Mapbox Annotation Plugin for Android (cuts down on boilerplate code that I wrote above): https://www.mapbox.com/android-docs/plugins/overview/annotation/#manager

    @langsmith

    Thanks for your comment.
    But i dont understand this line :

    Use that GeoJsonSource to create a CircleLayer.

    CircleLayer does not have any dependency to GeoJsonSource as i see

    And what id the source_id and layer_id ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Android

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions