Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.maptest.model;
- import com.mapbox.mapboxsdk.geometry.LatLng;
- import java.util.ArrayList;
- import java.util.List;
- public class Geofence {
- private LatLng center;
- private List<LatLng> polygon;
- private int radius;
- private boolean active = false;
- private Geofence(LatLng center, int radius){
- this.center = center;
- this.radius = radius;
- this.polygon = createGeofence(center,radius);
- }
- public List<LatLng> getPolygon(){
- return polygon;
- }
- public void setActive(boolean active){
- this.active = active;
- }
- public boolean isActive(){
- return active;
- }
- private List<LatLng> createGeofence(LatLng location, double radius) {
- int degreesBetweenPoints = 8; //45 sides
- int numberOfPoints = (int) Math.floor(360 / degreesBetweenPoints);
- double distRadians = radius / 6371000.0; // earth radius in meters
- double centerLatRadians = location.getLatitude() * Math.PI / 180;
- double centerLonRadians = location.getLongitude() * Math.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 * Math.PI / 180;
- double pointLatRadians = Math.asin(Math.sin(centerLatRadians) * Math.cos(distRadians) + Math.cos(centerLatRadians) * Math.sin(distRadians) * Math.cos(degreeRadians));
- double pointLonRadians = centerLonRadians + Math.atan2(Math.sin(degreeRadians) * Math.sin(distRadians) * Math.cos(centerLatRadians),
- Math.cos(distRadians) - Math.sin(centerLatRadians) * Math.sin(pointLatRadians));
- double pointLat = pointLatRadians * 180 / Math.PI;
- double pointLon = pointLonRadians * 180 / Math.PI;
- LatLng point = new LatLng(pointLat, pointLon);
- polygons.add(point);
- }
- return polygons;
- }
- public static class Builder{
- private LatLng center;
- private int radius;
- public Builder(){}
- public Builder addCenter(LatLng center){
- this.center = center;
- return this;
- }
- public Builder addRadius(int radius){
- this.radius = radius;
- return this;
- }
- public Geofence build(){
- return new Geofence(center,radius);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement