Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package com.example.maptest.model;
  2.  
  3. import com.mapbox.mapboxsdk.geometry.LatLng;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class Geofence {
  8.  
  9.     private LatLng center;
  10.     private List<LatLng> polygon;
  11.     private int radius;
  12.     private boolean active = false;
  13.  
  14.     private Geofence(LatLng center, int radius){
  15.         this.center = center;
  16.         this.radius = radius;
  17.         this.polygon = createGeofence(center,radius);
  18.     }
  19.  
  20.     public List<LatLng> getPolygon(){
  21.         return polygon;
  22.     }
  23.  
  24.     public void setActive(boolean active){
  25.         this.active = active;
  26.     }
  27.  
  28.     public boolean isActive(){
  29.         return active;
  30.     }
  31.  
  32.     private List<LatLng> createGeofence(LatLng location, double radius) {
  33.         int degreesBetweenPoints = 8; //45 sides
  34.         int numberOfPoints = (int) Math.floor(360 / degreesBetweenPoints);
  35.         double distRadians = radius / 6371000.0; // earth radius in meters
  36.         double centerLatRadians = location.getLatitude() * Math.PI / 180;
  37.         double centerLonRadians = location.getLongitude() * Math.PI / 180;
  38.         List<LatLng> polygons = new ArrayList<>(); //array to hold all the points
  39.         for (int index = 0; index < numberOfPoints; index++) {
  40.             double degrees = index * degreesBetweenPoints;
  41.             double degreeRadians = degrees * Math.PI / 180;
  42.             double pointLatRadians = Math.asin(Math.sin(centerLatRadians) * Math.cos(distRadians) + Math.cos(centerLatRadians) * Math.sin(distRadians) * Math.cos(degreeRadians));
  43.             double pointLonRadians = centerLonRadians + Math.atan2(Math.sin(degreeRadians) * Math.sin(distRadians) * Math.cos(centerLatRadians),
  44.                     Math.cos(distRadians) - Math.sin(centerLatRadians) * Math.sin(pointLatRadians));
  45.             double pointLat = pointLatRadians * 180 / Math.PI;
  46.             double pointLon = pointLonRadians * 180 / Math.PI;
  47.             LatLng point = new LatLng(pointLat, pointLon);
  48.             polygons.add(point);
  49.         }
  50.         return polygons;
  51.     }
  52.  
  53.     public static class Builder{
  54.         private LatLng center;
  55.         private int radius;
  56.  
  57.         public Builder(){}
  58.  
  59.         public Builder addCenter(LatLng center){
  60.             this.center = center;
  61.             return this;
  62.         }
  63.  
  64.         public Builder addRadius(int radius){
  65.             this.radius = radius;
  66.             return this;
  67.         }
  68.  
  69.         public Geofence build(){
  70.             return new Geofence(center,radius);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement