I am using Primefaces gmap to show markers and circle overlays on some markers. I want to toggle the circle overlay on a button click. Any idea how I can do that? I am using JSF 2.2 and Primefaces 3.4.2.
This is how I load the map in the jsf page.
<p:gmap center="21, 78" id="map"
zoom="2"
type="roadmap"
style="width:99%;height:90%;position:absolute"
model="#{SCNBean.supplyChainMapModel}"
widgetVar="mapVar">
This is how I load the markers and circle overlays in my backing bean.
#PostConstruct
public void viewSupplyChainNetwork(){
for (Node node : supplyChainNodes) {
LatLng coord = new LatLng(node.getLatitude(), node.getLongitude());
marker = new Marker(coord, node.getAddress(), node, getMarkerIcon(node.getNodeType()));
supplyChainMapModel.addOverlay(marker);
//adding circle overlay
Circle circle1 = new Circle(coord, riskval*10000);
circle1.setStrokeColor("#d93c3c");
circle1.setFillColor("#d93c3c");
circle1.setFillOpacity(0.7);
supplyChainMapModel.addOverlay(circle1);
}
Here is how the button on the map is created using javascript
function HeatMapControl(heatMapControlDiv, gmap) {
heatMapControlDiv.style.padding = '5px';
var controlUI = document.createElement('div');
controlUI.title = 'Heat Map';
heatMapControlDiv.appendChild(controlUI);
var controlText = document.createElement('div');
controlText.innerHTML = '<strong>Heat Map</strong>';
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI, 'click', function() {
heatmap.setMap(heatmap.getMap() ? null : gmap);
});
I want to make the circle overlay toggle on the click of a button on the map. How can I achieve this?
I would move the mapModel to a requestScoped Bean, let's call it SomeRequestScopedMapBean
it should have a reference to your current bean as a ManagedProperty
Your xhtml could work like this: (I don't think there is any need to create it with javascript, but I dit it with a commandButton)
<p:gmap
model="#{someRequestScopedMapBean.supplyChainMapModel}"
widgetVar="mapVar"
id="pgmapId">
<p:commandButton value="Toggle Circles"
actionListener="#{sCNBean.toggleShowCircles}"
ajax="true"
update="pgmapId"
/>
Here's the callback for your action, in your viewScoped Backing bean
public void toggleShowCircles(){
if(showCircles)
showCircles = false;
else
showCircles = true;
}
The MapModel construction is moved to your SomeRequestScopedMapBean
#PostConstruct
public void drawMapModel(){
for (Node node : sCNBean.supplyChainNodes) {
LatLng coord = new LatLng(node.getLatitude(), node.getLongitude());
marker = new Marker(coord, node.getAddress(), node, getMarkerIcon(node.getNodeType()));
supplyChainMapModel.addOverlay(marker);
if(sCNBean.showCircles){
//adding circle overlay
Circle circle1 = new Circle(coord, riskval*10000);
circle1.setStrokeColor("#d93c3c");
circle1.setFillColor("#d93c3c");
circle1.setFillOpacity(0.7);
supplyChainMapModel.addOverlay(circle1);
}
}
Related
Hi i am trying to simply drag an away3d mesh with the mouse but with no luck.
as i see it i need to convert the mouseX and MouseY to the 3d world coordinates
however, this is what i tried and it doesnt work. also i need it to be draged in enter frame or TIMER.
HELP PLEASE :)
PlayerHandle = new Mesh(new CubeGeometry(200, 100, 5));
_view.scene.addChild(PlayerHandle);
PlayerHandle.mouseEnabled = true;
PlayerHandle.addEventListener(MouseEvent3D.MOUSE_MOVE, HandlePlayerHandler);
public function HandlePlayerHandler(me3d:MouseEvent3D):void
{
PlayerHandle.x = me3d.scenePosition.x;
}
public function UpdateScene(e:Event):void
{
_view.render();
}
Away3D has built in class,
import away3d.tools.utils.Drag3D;
e.g.
private var _drag3D:Drag3D;
private var _sphere : Sphere;
//create sphere geometry here in a function
.
.
.
_drag3D = new Drag3D(_view, ObjectContainer3D(_sphere));
private function handleEnterFrame(e:Event) : void
{
_drag3D.updateDrag();
_view.render();
}
See this, Drag3DTest Class.
I have a very simple question.
Is there a way to add multiple polygons to PrimeFaces GMap?
If yes, can someone share a snippet/example?
If no, are there any alternatives to GMap in JSF?
Any help will be appreciated! Thanks
The first sentence in the link you provided gives you the answer:
Any number of polygons can be displayed on map.
I would do it the following way:
#ManagedBean
public class PolygonsView implements Serializable {
private MapModel polygonModel;
#PostConstruct
public void init() {
polygonModel = new DefaultMapModel();
//Shared coordinates
LatLng coord1 = new LatLng(36.879466, 30.667648);
LatLng coord2 = new LatLng(36.883707, 30.689216);
LatLng coord3 = new LatLng(36.879703, 30.706707);
//Polygon
Polygon polygon = new Polygon();
polygon.getPaths().add(coord1);
polygon.getPaths().add(coord2);
polygon.getPaths().add(coord3);
polygon.setStrokeColor("#FF9900");
polygon.setFillColor("#FF9900");
polygon.setStrokeOpacity(0.7);
polygon.setFillOpacity(0.7);
polygonModel.addOverlay(polygon);
//here it should be possible to add additional overlays
}
public MapModel getPolygonModel() {
return polygonModel;
}
}
The source code is also from the link you provided. Just create more Polygons and add them as overlay to your MapModel.
I want to store the previous location before dragging a marker using PrimeFaces GMap - Draggable Markers
How to do it ?
showcase : GMap - Draggable Markers :
http://www.primefaces.org/showcase/ui/gmapDraggableMarkers.jsf
You could store copies of the original Marker objects and then, in the onMarkerDrag handler, you could find the original Marker that corresponds to the one passed by the MarkerDragEvent.
I say "copies" because it is possible that Primefaces modifies the LatLng of original Marker instead of creating a new one (most likely). You can use the "data" or "title" attributes to give them IDs and correctly match the copied Markers.
I can probably explain this with code, let me know if you need help.
Edit:
To meet your requirement, I would adapt the Primefaces showcase example as follows (notice that it requires the Marker's title to be unique. If this is not possible, implement this using the data attribute of the Marker class.):
package org.primefaces.examples.view;
//...
public class MapBean implements Serializable {
private MapModel draggableModel;
private Map<String, LatLng> positions = new HashMap<String, LatLng>(); // new
public MapBean() {
draggableModel = new DefaultMapModel();
positions.put("Konyaalti", new LatLng(36.879466, 30.667648));
positions.put("Ataturk Parki", new LatLng(36.883707, 30.689216));
positions.put("Karaalioglu Parki", new LatLng(36.879703, 30.706707));
positions.put("Kaleici", new LatLng(36.885233, 30.702323));
for (Map.Entry<String, LatLng> e : positions.entrySet()) {
Marker m = new Marker(e.getValue(), e.getKey());
m.setDraggable(true);
draggableModel.addOverlay(m);
}
}
public void onMarkerDrag(MarkerDragEvent event) {
marker = event.getMarker();
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marker Dragged", "Lat:" + marker.getLatlng().getLat() + ", Lng:" + marker.getLatlng().getLng()));
// update position and get the old one
LatLng oldPosition = positions.put(marker.getTitle(), marker.getLatlng());
// ...
}
// ...
}
I am creating an android presentation app for a tablet that is projected on a screen. I have created a TouchEvent the adds a circle sprite so the audience can see where the presenter clicked on the screen. Here are the functions:
public function onTouchBegin(e:TouchEvent):void
{
var dot:Sprite = this.getCircle();
dot.x = e.stageX;
dot.y = e.stageY;
stage.addChild(dot);
dot.startTouchDrag(e.touchPointID, true);
dots[e.touchPointID] = dot;
}
public function onTouchEnd(e:TouchEvent):void
{
var dot:Sprite = this.dots[e.touchPointID];
stage.removeChild(dot);
delete this.dots[e.touchPointID];
}
private function getCircle(circumference:uint = 20):Sprite
{
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0x00AFF0, .3);
circle.graphics.drawCircle(0, 0, circumference);
circle.mouseChildren = true;
return circle;
}
This works great, but I have some movieclips on the stage that cannot be clicked because the touch event is hijacking the click event. How do I get the click event assigned to the movieclips to fire through the dynamically added sprite?
circle.mouseEnabled = false;
You can simply disable mouse interaction with the circle.
as a follow up to another question here:
i've build a custom contextmenu item in a flash application and had a problem with it not showing sometimes.
i found out that the issue was that another sprite was lying "on top" of the item with the custom contextmenu.
however, even with the "mouseEnabled" and "mouseChildren" properties set to false for the item "on top" i still cannot get the custom contextmenu to show...
any ideas? thanks!
ps: here is some code to see the problem:
var spr:Sprite=new Sprite();
spr.graphics.beginFill(0xff0000,1);
spr.graphics.drawRect(0,0,100,100);
addChild(spr);
var blocker:Sprite=new Sprite();
blocker.x=50
blocker.y=50
blocker.graphics.beginFill(0x00ff00,1);
blocker.graphics.drawRect(0,0,100,100);
addChild(blocker);
blocker.mouseEnabled=false
blocker.mouseChildren=false
var customContextMenu:ContextMenu = new ContextMenu();
var item:ContextMenuItem = new ContextMenuItem("custom item");
customContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler,false,0,true);
spr.contextMenu = customContextMenu;
function menuItemSelectHandler(cem:ContextMenuEvent) {
trace("hello context");
};
when the mouse is over the green rect, the custom contextmenuitem is not shown
The context menu for an object will be displayed only if the user right-clicks directly on that object itself, as far as I know.
I've simplified your problem in this code:
public class Test extends Sprite
{
public function Test()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var sprite:Sprite = new Sprite();
addChild(sprite);
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawRect(0, 0, 200, 200);
sprite.graphics.endFill();
var shape:Shape = new Shape();
addChild(shape);
shape.graphics.beginFill(0x0000FF, .6);
shape.graphics.drawRect(100, 100, 200, 200);
shape.graphics.endFill();
setUpContextMenu(sprite);
}
private function setUpContextMenu(target:InteractiveObject):void
{
var menu:ContextMenu = new ContextMenu();
target.contextMenu = menu;
var item:ContextMenuItem = new ContextMenuItem("About Us");
menu.customItems.push(item);
}
}
When you right-click on the area where the red and blue squares overlap, you don't get the custom context menu.
Here's a possible solution, where I've modified only the setUpContextMenu() function:
private function setUpContextMenu(target:InteractiveObject):void
{
var menu:ContextMenu = new ContextMenu();
this.contextMenu = menu;
var item:ContextMenuItem = new ContextMenuItem("About Us");
var handler:Function = function (event:ContextMenuEvent):void {
// Start with empty menu.
var items:Array = [];
if (event.mouseTarget == target) {
// Directly right-clicked on target. Add custom item.
items = [item];
} else if (event.mouseTarget is DisplayObjectContainer) {
var o:DisplayObjectContainer
= DisplayObjectContainer(event.mouseTarget);
var pt:Point = o.localToGlobal(new Point(o.mouseX, o.mouseY));
var arr:Array = o.getObjectsUnderPoint(pt);
// One of the mouse target's descendants is our target,
// directly under the pointer. Add custom item.
if (arr.indexOf(target) != -1)
items = [item];
}
menu.customItems = items;
};
menu.addEventListener(ContextMenuEvent.MENU_SELECT, handler);
}
Here I'm assigning the context menu to the application itself. On the "menuSelect" event, I customize it based on whether the mouse pointer is somewhere above the red square.