In javascript I can create a new object from a constructor, using the new operator. How can this be done in ScalaJS?
In answer to my own question, I discovered from here - Creating custom DOM events with scalajs
I'm using require within NodeJS, actually Atom-Shell but that's basically NodeJS.
The Javascript:
https://github.com/atom/atom-shell/blob/master/docs/api/browser-window.md
var BrowserWindow = require('browser-window');
var win = new BrowserWindow({ width: 800, height: 600, show: false });
The Scala equivalent is:
import js.Dynamic.{ global => g }
val browserWindow = js.Dynamic.newInstance(
g.require("browser-window").asInstanceOf[js.Dynamic])(
js.Dynamic.literal(width = 800, height = 600, show = false)
)
Related
I'm trying to move a client side method on the backend:
The client side method is using TurfJS and Leaflet to perform some operations. The only problem is that server side (nodejs) window is not available, hence I cannot use Leaflet.
I'm looking for a way to transform this piece of code in the vanilla Turf equivalent:
const pointA = turf.point([originCoords.lng, originCoords.lat]);
const pointB = turf.destination.default(pointA, 50, 45, { units: 'kilometers' });
// here I'm using leaflet to get a BBox
const latLngBounds = L.latLngBounds(
L.latLng(pointA.geometry.coordinates[1],
pointA.geometry.coordinates[0]),
L.latLng(pointB.geometry.coordinates[1], pointB.geometry.coordinates[0]),
);
// using that BBox I then create the rectangle, again with leaflet
tile = L.rectangle(latLngBounds);
I'm still newbie with this whole GeoJSON thing, and maybe I'm missing something, can someone help me?
Here is the code that uses Turf.JS only.
var turf = require('#turf/turf');
var pointA = turf.point([-75.343, 39.984]);
var pointB = turf.destination(pointA, 50, 45, { units: 'kilometers' });
//create a bbox that extends to include all the features
var bbx = turf.bbox(turf.featureCollection([pointA, pointB]));
var pgn = turf.bboxPolygon(bbx); //convert it to Polygon feature
console.log(JSON.stringify(pgn)); //output to console
The output:
{"type":"Feature","properties":{},
"geometry":{
"type":"Polygon",
"coordinates":[[
[-75.343,39.984],
[-74.92609,39.984],
[-74.92609,40.3012],
[-75.343,40.3012],
[-75.343,39.984]]]
}
}
Edit 1
Add ability to run the code:
//var turf = require('#turf/turf');
var pointA = turf.point([-75.343, 39.984]);
var pointB = turf.destination(pointA, 50, 45, { units: 'kilometers' });
//create a bbox that extends to include all the features
var bbx = turf.bbox(turf.featureCollection([pointA, pointB]));
var pgn = turf.bboxPolygon(bbx); //convert it to Polygon feature
console.log(JSON.stringify(pgn)); //output to console
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.min.js"></script>
I am able to draw the Map with maneuverPoints.
Below is the screen shot for map
Below is the code for the routing.
var maneuverPoints = this.model.get("maneuverPoints");
if (maneuverPoints) {
var routePoints = [];
_.each(maneuverPoints, function (point) {
routePoints.push(new MsMaps.Location(point.latitude, point.longitude));
});
var routeOptions = {
strokeColor: new MsMaps.Color(1, 65, 255, 35),
strokeThickness: 3
};
var routeShape = new MsMaps.Polyline(routePoints, routeOptions);
map.entities.push(routeShape);
}
Now I am trying to implement clustering and I am able to do it as below:
The pink pushpins are the clusters.
Code for clustering is as below:
Microsoft.Maps.loadModule("Microsoft.Maps.Clustering", function () {
var clusterLayer = new Microsoft.Maps.ClusterLayer(pushpins);
map.layers.insert(clusterLayer);
clusterLayer.setPushpins(pushpins);
map.setView(viewOptions);
var maneuverPoints = this.model.get("maneuverPoints");
if (maneuverPoints) {
var routePoints = [];
_.each(maneuverPoints, function (point) {
routePoints.push(new MsMaps.Location(point.latitude, point.longitude));
});
var routeOptions = {
strokeColor: new MsMaps.Color(1, 65, 255, 35),
strokeThickness: 3
};
var routeShape = new MsMaps.Polyline(routePoints, routeOptions);
map.entities.push(routeShape);
}
I am not able to do clustering and routing at same time. Is it possible to do both at same time?
Your code isn't doing routing by the looks of things, it is simply taking route line points and rendering them as a polyline. I don't see the directions module being used in your code. If you are using that directions module, why don't you simply use the built in rendering? You can customize how it looks. If you continue using a polyline as you are, consider adding it to a layer, it will make it easier to manage later.
That said, the code you provided is simply clustering and drawing a polyline. This works fine when I test this scenario.
Any reason why you are calling setPushpins in the cluster layer? You already passed the pushpins in when creating the layer, no need to pass them in again.
Is it possible to create a button which when pressed it replaces an existing object on the screen to another object in Smartface App Studio?
I've tried using the Onshow event on the text however it wasn't successful.
Thankyou
It is possible to replace an existing object to another object. You should create objects and add to page both of them but visible property of an object should be false on start and then you can change visible of object when button is pressed.I create a simple example for you:
var btn = new SMF.UI.TextButton({
top : "80%",
left : "10%",
onPressed : page1_btn_onPressed
});
Pages.Page1.add(btn);
var myImage = new SMF.UI.Image({
top: "20%",
left: "15%",
height: "20%",
width: "70%",
image: "default.png",
imageFillType: SMF.UI.ImageFillType.stretch,
visible : true
});
Pages.Page1.add(myImage);
var myImage2 = new SMF.UI.Image({
top: "20%",
left: "15%",
height: "20%",
width: "70%",
image: "icon.png",
imageFillType: SMF.UI.ImageFillType.stretch,
visible : false
});
Pages.Page1.add(myImage2);
function page1_btn_onPressed(e) {
myImage.visible= false;
myImage2.visible = true;
}
I need to create an Directed Graph and an image displaying this Graph.
I tried using DirectedGraph which works just fine to create the Graph, it´s internally stored correctly, I tested this but I fail in creating an image from it to Display in an E4 RCP Application.
This is my code:
import org.jgraph.JGraph;
import org.jgrapht.DirectedGraph;
import org.jgrapht.ext.JGraphModelAdapter;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
DirectedGraph <String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
addVertexes();
addEdges();
//Create image from graph
JGraphModelAdapter<String, DefaultEdge> graphModel = new JGraphModelAdapter<String, DefaultEdge>(graph);
JGraph jgraph = new JGraph (graphModel);
BufferedImage img = jgraph.getImage(Color.WHITE, 5);
but apparently img is always null. Why is that so and how can I change this to work properly?
Just read about JGraphX and tried using that, so for me it works just fine! This is an example of my code now (example with reduced Vertices and Edges).
mxGraph graphMx = new mxGraph();
graphMx.insertVertex(graphMx.getDefaultParent(), "Start", "Start", 0.0, 0.0, 50.0, 30.0, "rounded");
graphMx.insertVertex(graphMx.getDefaultParent(), "Ende", "Ende", 0.0, 0.0, 50.0, 30.0, "rounded");
graphMx.insertEdge(graphMx.getDefaultParent(), null, "", ((mxGraphModel)graphMx.getModel()).getCell("Start"), ((mxGraphModel)graphMx.getModel()).getCell("Ende"));
mxIGraphLayout layout = new mxHierarchicalLayout(graphMx);
layout.execute(graphMx.getDefaultParent());
BufferedImage image = mxCellRenderer.createBufferedImage(graphMx, null, 1, Color.WHITE, true, null);
return image;
My answer is similar to #HexFlex, but i got this to work the same way, although i'm not sure how to customize the drawing:
String GRAPH_FILE_PATH = "somwhere/u/want.png";
public static <V, E> File drawGraph(Graph<V, E> graph) throws IOException {
JGraphXAdapter<V, E> graphAdapter = new JGraphXAdapter<V, E>(graph);
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, new Color(0f,0f,0f,.5f), true, null);
File imgFile = new File(GRAPH_FILE_PATH);
ImageIO.write(image, "PNG", imgFile);
return imgFile;
}
Edit:
Btw, the code is from https://www.baeldung.com/jgrapht, i just refactored it into a function
you can also put it on a JFRAME and generate an image from that frame.
-Jan
/// <reference path="openlayers.d.ts" />
class MapComponent {
element: HTMLElement;
map: OpenLayers.Map;
constructor(element: HTMLElement) {
// Setup our map object
this.element = element;
this.map = new OpenLayers.Map(this.element);
}
init() {
// Setup our two layer objects
var osm_layer_map = new OpenLayers.Layer.OSM("OSM");
// Add layers to the map
this.map.addLayers([osm_layer_map]);
// Add a layer switcher control
this.map.addControl(new OpenLayers.Control.LayerSwitcher({}));
// Zoom the map to the max extent
if (!this.map.getCenter()) {
this.map.zoomToMaxExtent();
}
}
}
window.onload = () => {
var el = document.getElementById('map');
var mc = new MapComponent(el);
mc.init();
}
I have the above piece of code to work with a simple HTML file with only 1 of ID, 'map' with style: height and width # 500px.
I have tried several other ways to get the map to display but so far all i got was a white page (blank).
Can anybody point me in the right direction?
Solutions tried so far:
using jquery with ready function
replace window.onload with a call direct from the html, <script><script/>
place document.getElementById() in the new OpenLayers.Map(here); when first creating this.map
placing the window.onload call above and below (currently)
using export class or public init() or both
As of now, I just want it to work.
Seems that creating the map with the element provided and later defining the options doesn't work.
Instead either initialize the map with options
var options = {
projection: "EPSG:3857",
maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000),
center: new OpenLayers.LonLat(-12356463.476333, 5621521.4854095)
};
this.map = new OpenLayers.Map(this.element, options);
Or call this.map.render(this.element) at the end of your init method.
Also make sure your div is actually visible and has some size specified, otherwise it might be not visible...