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]
This commit is contained in:
Haoming Yin
2017-05-10 20:52:46 +12:00
parent 6f1b0b06c3
commit 189ba93e64
4 changed files with 129 additions and 0 deletions
@@ -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;
}
}
@@ -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();
}
}
@@ -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);
}
}