Reference to this in anonymous function Dart.js - dart

Is it possible to get a reference to this of creating an object in an anonymous function:
var marker = Marker(
icon: BitmapDescriptor.defaultMarker,
onTap: () {
/// <--- here I need a reference to the marker
});
this is not working as it is outer object, is it possible to cast something like this#Marker

You need to split the variable declaration and initialization because the initialization code can't reference the variable it initializes.
If your code is inside a function, you can use
Marker marker;
marker = Marker(
icon: BitmapDescriptor.defaultMarker,
onTap: () {
/// <--- here I need a reference to the marker
});

You cannot refer to this outside of the class itself.
You could try using marker to refer to the marker being created, but that will not work here because a variable cannot be referred to inside its own initializer.
So, you have to split the variable declaration from the initialization:
Marker marker;
marker = Marker(
icon: BitmapDescriptor.defaultMarker,
onTap: () {
marker.something();
});

Related

A-Frame + AR.js: How to add objects dynamically in Arjs when tapping on screen?

I'm currently working on an A-Frame AR app by using ar.js (I have to use ar.js). The goal is to dynamically add objects to the scene when tapping on the scene. Unfortunately nothing seems to work out. I managed to change the colors of an existing object when tapping on the screen but not adding a new one. Could someone please help me out here? I would be really grateful!
AFRAME.registerComponent('cursor-listener', {
init: function () {
var currentEl = this.el;
currentEl.addEventListener("click", function (evt) {
console.log("I was clicked!");
currentEl.setAttribute('material', {"color": "blue"});
var el = document.createElement("a-entity");
el.setAttribute('geometry', { "primitive": "sphere", "radius": "1" });
el.setAttribute('position', this.el.object3D.position)
const randomYRotation = Math.random() * 360
el.setAttribute('rotation', `0 ${randomYRotation} 0`)
el.setAttribute('visible', 'true')
el.setAttribute('shadow', {receive: false})
this.el.sceneEl.appendChild(el);
});
},
});
this.el.sceneEl.appendChild(el);
You should append the element to the marker node, and treat it as your "root" element - as it's the one being tracked when detected by camera:
<script>
AFRAME.registerComponent('cursor-listener', {
init:function () {
const marker = document.querySelector("a-marker")
var el = document.createElement("a-sphere");
marker.appendChild(el);
}
})
</script>
<!-- scene and stuff -->
<a-marker preset="hiro>
</a-marker>

Flutter: How to keep gesture events state after opening a simple dialog

I have a grid of Images and I want to open a simple dialog when I long press an Image and to be closed automatically when my finger no longer contacts with the screen (like Instagram quick image preview).
I attached LongPress event to all the images and it works fine so a dialog opens up when I long press an image however when I put my finger up nothing happens even though I attached events like onTapUp, onLongPressEnd, onPointerUp Because of the new opened dialog, All of those events are lost and no longer fires up.
I tried to add the pointer up events to the opened dialog instead but there is a catch, I must tap and release again in order to make it work because Flutter unable to recognize that my finger is already in contact with screen and the opened dialog caused flutter to forget about this fact.
You can insert an OverlayEntry into the Overlay stack by using Overlay.of(context).insert(overlayEntry).
In this overlay, you can catch gestures when required and take actions accordingly. As overlays always sit on top of anything else, the dialog will not cancel your long press gesture and you will be able to respond to longPressEnd.
You will only need to calculate which image has been pressed or use the Offset's provided by onTapDown and the position of the images.
To get the global position of your images, you can assign GlobalKey's to your images and get their global positions in the following way:
final RenderBox renderBox = globalKey.currentContext.findRenderObject() as RenderBox;
final Offset position = renderBox.localToGlobal(Offset.zero);
final Size size = renderBox.size;
To get the position of your long press, you will need to store the position of onTapDown:
onTapDown: (details) => position = details.globalPosition
Now you have everything you need to figure out which bounds the long press happened in.
I found a way to make it work. It can be done with Overlay Widget.
In the widget with GestureDetector, when onLongPress is called, create an OverlayEntry object with your dialog, and insert it into Overlay.
When onLongPressEnd is called, call the remove function of OverlayEntry object.
// Implement a function to create OverlayEntry
OverlayEntry getMyOverlayEntry({
#required BuildContext context,
SomeData someData,
}) {
return OverlayEntry(
builder: (context) {
return AlertDialog(child: SomeWidgetAgain());
}
);
}
// In the widget where you want to support long press feature
OverlayEntry myOverayEntry;
GestureDetector(
onLongPress: () {
myOverayEntry = getMyOverlayEntry(context: context, someData: someData);
Overlay.of(context).insert(myOverayEntry);
},
onLongPressEnd: (details) => myOverayEntry?.remove(),
child: SomeWidgerHere(),
)
Here's the gist on Github:
https://gist.github.com/plateaukao/79aa39854dc4eabf1220bdfa9a0334b6
You can use AnimatedContainer and put a GestureDetector inside.
change width and height using setState and it's done.
Center(
child: AnimatedContainer(
width: containerWidth,
height: containerHeight,
color: Colors.red,
duration: Duration(seconds: 1),
child: GestureDetector(
onLongPress: (){
print("Long Press");
setState(() {
containerWidth = 200;
containerHeight = 200;
});
},
onLongPressUp: (){
print("On Long Press UP");
setState(() {
containerWidth = 100;
containerHeight = 100;
});
},
),
),
)

OpenLayers 3 - Get applied style for rendered feature

Is it possible to get the applied style for a selected feature? My layer uses a style function instead of an ol.style.Style. When I call myFeature.getStyle() it returns the function.
Updating my question with some things that I have tried so far based on responses here and elsewhere.
I ended up creating a select style function for each layer.
This doesn't work, no style is returned:
selectInteraction = new ol.interaction.Select( {
style: function( feature, resolution ) {
if ( feature.selectStyle ) {
return feature.selectStyle;
}
},
layers: mylayers
} );
Also, with the above I get the following error:
Uncaught TypeError: style.getImage is not a function
ol.renderer.vector.renderFeature #ol.js:38455
ol.renderer.canvas.VectorLayer.renderFeature #ol.js:44202
renderFeature #ol.js:44148
ol.renderer.canvas.VectorLayer.prepareFrame #ol.js:44164
ol.renderer.canvas.Map.renderFrame #ol.js:45268
ol.Map.renderFrame_ #ol.js:52034
(anonymous function) #ol.js:50898
This does work, the feature is rendered using my select style. However, I don't want to have to manage re-setting styles, so it really needs to be implemented in the selectInteraction:
evt.selected.forEach( function( evt ) {
evt.setStyle( evt.selectStyle );
}, this );
My workaround was to create the style separately from the feature and then use setStyle() to apply it. I also saved the style elsewhere in the feature so it could be retrieved later.
let myStyle = new ol.style.Style({
image: new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'cyan'
}),
stroke: new ol.style.Stroke({
color: 'black',
width: 1
}),
points: 4,
radius: 9,
angle: Math.PI / 4
}),
});
myFeature.setStyle(myStyle);
myFeature.set('myStyle', myStyle); // save it for later use
During the 'select' process simply use:
feature.get('myStyle');
to retrieve the style that was applied to the feature.
You should call the function with the feature (and optionally the resolution if your style function depends on it). So something like: myFeature.getStyle().call(this, feature); or myFeature.getStyle().call(this, feature, resolution);
I think you must use myFeature.getStyleFunction() instead of myFeature.getStyle().
Good Luck.

Is it possible to drag the marker in highcharts?

Is it possible to drag the marker, currently clicking on the charts able to get new marker using the plotline in highcharts!
is it possible to hold/grab the marker line and drag it on the chart?
CLICK ON THE PLOTLINE
var chart = this.xAxis[0];
chart.removePlotLine('plot-line-1');
chart.addPlotLine({
value: event.xAxis[0].value,
color: '#FF0000',
width: 2,
id: 'plot-line-1'
});
Check out Highcharts' draggable plugin: jsfiddle.net/highcharts/AyUbx/
See this: http://api.highcharts.com/highcharts#xAxis.plotLines.events
Supported properties are click, mouseover, mouseout, mousemove. These events are not enough to implement the dragging option for the marker(PlotLine). Atleast not in a neat way.
You can do it by assigning an on event to the svg element:
http://jsfiddle.net/yem93w5o/1/
var line, clickY;
var start = function (e) {
$(document).bind({
'mousemove.line': step,
'mouseup.line': stop
});
clickY = e.pageY - line.translateY;
}
var step = function (e) {
line.translate(0, e.pageY - clickY)
}
var stop = function () {
$(document).unbind('.line');
}
(...)
line = chart.highcharts().yAxis[0].plotLinesAndBands[0].svgElem.translate(0,0).on('mousedown', start);

add marker with Google-Maps-for-Rails

I was playing with https://github.com/apneadiving/Google-Maps-for-Rails and
i'd like to clear all the marker on a map and add a new one at the position clicked by the user on the map, I added the following code in my page
<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
google.maps.event.addListener(Gmaps4Rails.map, 'click', function(object){
alert(object.latLng);
});
</script>
And in this way i can see an alert with the lat and long value, now how can i delete the old markers and create a new marker and place it on the map?
If you want to clear markers created by gmaps4rails, use this js function:
Gmaps4Rails.clear_markers();
otherwise, loop through your markers and make marker.setMap(null)
Well, the following function removes all markers and add a new one where user clicks:
var marker = null;
function gmaps4rails_callback() {
Gmaps4Rails.clear_markers();
if (marker != null) { marker.setMap(null); }
google.maps.event.addListener(Gmaps4Rails.map, 'click', function(object){ marker = new google.maps.Marker({position: object.latLng, map: Gmaps4Rails.map});});
}
Notes:
You could use whatever logic you desire in the js code to create only one marker or send it's coordinates through ajax.
latitude can be retrieved with: object.latLng.lat(), longitude with: object.latLng.lng()
Another way to add markers is to use the add_marker function described here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Dynamic-%28or-Ajax%29-map-refresh

Resources