From 189ba93e642a28b5f5e3cbe70cc2ed4fa66474ed Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Wed, 10 May 2017 20:52:46 +1200 Subject: [PATCH] Created a canvas map class to fetch map image from google - also added Bound class to encapsulate map boundary. - created TestMapView and its controller just for testing. #story[928] --- src/main/java/seng302/models/map/Bound.java | 44 +++++++++++++++++ .../java/seng302/models/map/CanvasMap.java | 49 +++++++++++++++++++ .../seng302/models/map/TestMapController.java | 23 +++++++++ src/main/resources/views/TestMapView.fxml | 13 +++++ 4 files changed, 129 insertions(+) create mode 100644 src/main/java/seng302/models/map/Bound.java create mode 100644 src/main/java/seng302/models/map/CanvasMap.java create mode 100644 src/main/java/seng302/models/map/TestMapController.java create mode 100644 src/main/resources/views/TestMapView.fxml diff --git a/src/main/java/seng302/models/map/Bound.java b/src/main/java/seng302/models/map/Bound.java new file mode 100644 index 00000000..aa700423 --- /dev/null +++ b/src/main/java/seng302/models/map/Bound.java @@ -0,0 +1,44 @@ +package seng302.models.map; + +/** + * The Bound class is to represent square territorial bounds on a map. It contains + * four extremity double values(N, E, S, W). N and S are represented as latitudes + * in radians. E and W are represented as longitudes in radians. + * + * Created by Haoming on 10/5/17 + */ +public class Bound { + + private double north, east, south, west; + + public Bound(double north, double east, double south, double west) { + this.north = north; + this.east = east; + this.south = south; + this.west = west; + } + + public double getCentreLat() { + return (north + south) / 2; + } + + public double getCentreLng() { + return (east + west) / 2; + } + + public double getNorth() { + return north; + } + + public double getEast() { + return east; + } + + public double getSouth() { + return south; + } + + public double getWest() { + return west; + } +} diff --git a/src/main/java/seng302/models/map/CanvasMap.java b/src/main/java/seng302/models/map/CanvasMap.java new file mode 100644 index 00000000..4a524e49 --- /dev/null +++ b/src/main/java/seng302/models/map/CanvasMap.java @@ -0,0 +1,49 @@ +package seng302.models.map; + +import javafx.scene.image.Image; + +import javax.imageio.ImageIO; +import javax.net.ssl.HttpsURLConnection; +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; + +public class CanvasMap { + + private Bound bound; + private double width, height; // desired image size + private int zoom; + private String KEY = "AIzaSyC-5oOShMCY5Oy_9L7guYMPUPFHDMr37wE"; + + public CanvasMap(Bound bound, double width, double height) { + this.bound = bound; + this.width = width; + this.height = height; + } + + public Image getMapImage() { + + try { + System.out.println(getRequest()); + URL url = new URL(getRequest()); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + + return new Image(connection.getInputStream()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private String getRequest() { + zoom = 14; + StringBuilder sb = new StringBuilder(); + sb.append("https://maps.googleapis.com/maps/api/staticmap?"); + sb.append(String.format("center=%f,%f", bound.getCentreLat(), bound.getCentreLng())); + sb.append(String.format("&zoom=%d", zoom)); + sb.append(String.format("&size=%.0fx%.0f&scale=2", width / 2, height / 2)); + sb.append(String.format("&key=%s", KEY)); + return sb.toString(); + } +} diff --git a/src/main/java/seng302/models/map/TestMapController.java b/src/main/java/seng302/models/map/TestMapController.java new file mode 100644 index 00000000..720dd408 --- /dev/null +++ b/src/main/java/seng302/models/map/TestMapController.java @@ -0,0 +1,23 @@ +package seng302.models.map; + +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; + +import java.net.URL; +import java.util.ResourceBundle; + +public class TestMapController implements Initializable{ + + @FXML + private Canvas mapCanvas; + + @Override + public void initialize(URL location, ResourceBundle resources) { + GraphicsContext gc = mapCanvas.getGraphicsContext2D(); + Bound bound = new Bound(57.662943, 11.848501, 57.673945, 11.824966); + CanvasMap canvasMap = new CanvasMap(bound, 1280, 960); + gc.drawImage(canvasMap.getMapImage(), 0, 0, 1280, 960); + } +} diff --git a/src/main/resources/views/TestMapView.fxml b/src/main/resources/views/TestMapView.fxml new file mode 100644 index 00000000..84df98f3 --- /dev/null +++ b/src/main/resources/views/TestMapView.fxml @@ -0,0 +1,13 @@ + + + + + + + + + + + + +