Hello,
I'm here to ask for your help. The question is I want to use the GMap3 addons to show graphically a journey undertaken by a vehicle through GPolyline GMap, but fails to succeed. Imitating as done for you with the Location, create a new component with corresponding .css and .html files besides the Factory class, but I can not display the map in question. I attached, so if they have time to see, the Patch generated with the changes in the Addons Isis-Wicket-Gmap3 a trial and also, the .xls file from where it currently and provisionally I get, the route I want to show. >From already thank you very much for your time and apologize for my very bad English. Regards.
diff --git a/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/applib/Routeable.java b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/applib/Routeable.java new file mode 100755 index 0000000..78a5aad --- /dev/null +++ b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/applib/Routeable.java @@ -0,0 +1,10 @@ +package org.isisaddons.wicket.gmap3.cpt.applib; + +import java.util.List; + +import org.wicketstuff.gmap.api.GPoint; + +public interface Routeable { + + List<GPoint> getRoute(); +} diff --git a/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.css b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.css new file mode 100755 index 0000000..40269d5 --- /dev/null +++ b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.css @@ -0,0 +1,29 @@ +/* + * Copyright 2013 Dan Haywood + * + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +.collectionContentsAsRouteables>div { + height: 600px; +} + +.collectionContentsAsRouteables { + margin-top: 0; + padding-top: 45px; +} + +.collectionContentsAsRouteables .collection-contents-as-routeables-invisible { + display:visibility; +} diff --git a/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.html b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.html new file mode 100755 index 0000000..7c77016 --- /dev/null +++ b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.html @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Copyright 2013 Dan Haywood + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +either express or implied. See the License for the specific +language governing permissions and limitations under the License. +--> +<html xmlns:wicket="http://wicket.apache.org"> + <body> + <wicket:panel> + <div class="collectionContentsAsRouteables"> + <div wicket:id="mapa" class="map" style="margin-left: auto; margin-right: auto;">[map here]</div> + </div> + </wicket:panel> + </body> +</html> diff --git a/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.java b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.java new file mode 100755 index 0000000..3ce2351 --- /dev/null +++ b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteables.java @@ -0,0 +1,166 @@ +package org.isisaddons.wicket.gmap3.cpt.ui; + +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import org.apache.isis.core.metamodel.adapter.ObjectAdapter; +import org.apache.isis.viewer.wicket.model.models.EntityCollectionModel; +import org.apache.isis.viewer.wicket.model.models.EntityModel; +import org.apache.isis.viewer.wicket.model.models.ImageResourceCache; +import org.apache.isis.viewer.wicket.model.models.PageType; +import org.apache.isis.viewer.wicket.ui.pages.PageClassRegistry; +import org.apache.isis.viewer.wicket.ui.panels.PanelAbstract; +import org.apache.isis.viewer.wicket.ui.panels.PanelUtil; +import org.apache.wicket.AttributeModifier; +import org.apache.wicket.Component; +import org.apache.wicket.Page; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.behavior.AttributeAppender; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.ResourceReference; +import org.isisaddons.wicket.gmap3.cpt.applib.Routeable; +import org.wicketstuff.gmap.GMap; +import org.wicketstuff.gmap.api.GEvent; +import org.wicketstuff.gmap.api.GEventHandler; +import org.wicketstuff.gmap.api.GLatLng; +import org.wicketstuff.gmap.api.GPoint; +import org.wicketstuff.gmap.api.GPolyline; + +public class CollectionOfEntitiesAsRouteables extends +PanelAbstract<EntityCollectionModel> { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private static final String ID_MAP = "mapa"; + + private static final String INVISIBLE_CLASS = "collection-contents-as-routeables-invisible"; + + public CollectionOfEntitiesAsRouteables(final String id, + final EntityCollectionModel model) { + super(id, model); + buildGui(); + } + + private void buildGui() { + final EntityCollectionModel model = getModel(); + final List<ObjectAdapter> adapterList = model.getObject(); + + final GMap map = new GMap(ID_MAP); + map.setStreetViewControlEnabled(true); + map.setScaleControlEnabled(true); + map.setScrollWheelZoomEnabled(true); + map.setPanControlEnabled(true); + map.setDoubleClickZoomEnabled(true); + + for (ObjectAdapter adapter : adapterList) { + final GLatLng latLng; + latLng = asGLatLng(((Routeable) adapter).getRoute().get(0)); + if (latLng != null) { + map.setCenter(latLng); + break; + } + } + + addOrReplace(map); + applyCssVisibility(map, true); + + addRoute(map, adapterList); + } + + private void addRoute(final GMap map, final List<ObjectAdapter> adapterList) + { + //List<GLatLng> glatLngsToShow = Lists.newArrayList(); + for (ObjectAdapter adapter : adapterList) { + + final GPolyline gPolyline = createGPolyline(map, adapter); + if(gPolyline != null) { + map.addOverlay(gPolyline); + addClickListener(gPolyline, adapter); + // glatLngsToShow.add(gMarker.getLatLng()); + } + } + } + + private GPolyline createGPolyline(final GMap map, final ObjectAdapter adapter) { + // Asumir adapter como lista de Locations + final List<GLatLng> puntos = new ArrayList<GLatLng>(); + final Routeable routeable = (Routeable) adapter; + for (final GPoint point : routeable.getRoute()) + puntos.add(asGLatLng(point)); + + return new GPolyline("RED", 0, 0.5f, (GLatLng[])puntos.toArray()); + } + + private GLatLng asGLatLng(final GPoint point) { + return point!=null?new GLatLng(point.getLatitude(), point.getLongitude()):null; + } + + private static void applyCssVisibility(final Component component, final boolean visible) { + final AttributeModifier modifier = + visible + ? new AttributeModifier("class", String.valueOf(component.getMarkupAttributes().get("class")).replaceFirst(INVISIBLE_CLASS, "")) + : new AttributeAppender("class", " " + + INVISIBLE_CLASS); + component.add(modifier); + } + + private void addClickListener(final GPolyline gPolyline, ObjectAdapter adapter) { + final Class<? extends Page> pageClass = getPageClassRegistry() + .getPageClass(PageType.ENTITY); + final PageParameters pageParameters = EntityModel.createPageParameters( + adapter); + + gPolyline.addListener(GEvent.click, new GEventHandler() { + private static final long serialVersionUID = 1L; + + @Override + public void onEvent(AjaxRequestTarget target) { + setResponsePage(pageClass, pageParameters); + } + }); + } + + private ResourceReference determineImageResource(ObjectAdapter adapter) { + ResourceReference imageResource = null; + if (adapter != null) { + imageResource = getImageCache().resourceReferenceFor(adapter); + } + return imageResource; + } + + @Override + protected void onModelChanged() { + buildGui(); + } + + ////////////////////////////////////////////// + // Dependency Injection + ////////////////////////////////////////////// + + @Inject + private ImageResourceCache imageResourceCache; + + @Inject + private PageClassRegistry pageClassRegistry; + + protected ImageResourceCache getImageCache() { + return imageResourceCache; + } + + protected PageClassRegistry getPageClassRegistry() { + return pageClassRegistry; + } + + @Override + public void renderHead(IHeaderResponse response) { + super.renderHead(response); + + PanelUtil.renderHead(response, CollectionOfEntitiesAsRouteables.class); + } +} diff --git a/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteablesFactory.java b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteablesFactory.java new file mode 100755 index 0000000..ae16864 --- /dev/null +++ b/cpt/src/main/java/org/isisaddons/wicket/gmap3/cpt/ui/CollectionOfEntitiesAsRouteablesFactory.java @@ -0,0 +1,99 @@ +package org.isisaddons.wicket.gmap3.cpt.ui; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.UnknownHostException; + +import org.apache.isis.core.metamodel.spec.ObjectSpecification; +import org.apache.isis.core.metamodel.spec.SpecificationLoader; +import org.apache.isis.core.runtime.system.context.IsisContext; +import org.apache.isis.viewer.wicket.model.models.EntityCollectionModel; +import org.apache.isis.viewer.wicket.ui.CollectionContentsAsFactory; +import org.apache.isis.viewer.wicket.ui.ComponentFactoryAbstract; +import org.apache.isis.viewer.wicket.ui.ComponentType; +import org.apache.wicket.Component; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; +import org.isisaddons.wicket.gmap3.cpt.applib.Routeable; + +public class CollectionOfEntitiesAsRouteablesFactory extends ComponentFactoryAbstract implements CollectionContentsAsFactory { + + /** + * + */ + private static final long serialVersionUID = 7940581460663515229L; + + private static final String ID_MAP = "map"; + + private boolean determinedWhetherInternetReachable; + private boolean internetReachable; + + public CollectionOfEntitiesAsRouteablesFactory() { + super(ComponentType.COLLECTION_CONTENTS, ID_MAP); + } + + @Override + public ApplicationAdvice appliesTo(IModel<?> model) { + + if(!internetReachable()) { + return ApplicationAdvice.DOES_NOT_APPLY; + } + + if (!(model instanceof EntityCollectionModel)) { + return ApplicationAdvice.DOES_NOT_APPLY; + } + + EntityCollectionModel entityCollectionModel = (EntityCollectionModel) model; + + ObjectSpecification typeOfSpec = entityCollectionModel.getTypeOfSpecification(); + ObjectSpecification routeableSpec = getSpecificationLoader().loadSpecification(Routeable.class); + return appliesIf(typeOfSpec.isOfType(routeableSpec)); + } + + private boolean internetReachable() { + if(!determinedWhetherInternetReachable) { + internetReachable = isInternetReachable(); + determinedWhetherInternetReachable = true; + } + return internetReachable; + } + + /** + * Tries to retrieve some content, 1 second timeout. + */ + private static boolean isInternetReachable() + { + try { + final URL url = new URL("http://www.google.com"); + final HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection(); + urlConnect.setConnectTimeout(1000); + urlConnect.getContent(); + urlConnect.disconnect(); + } catch (UnknownHostException e) { + return false; + } catch (IOException e) { + return false; + } + return true; + } + + public Component createComponent(String id, IModel<?> model) { + EntityCollectionModel collectionModel = (EntityCollectionModel) model; + return new CollectionOfEntitiesAsRouteables(id, collectionModel); + } + + protected SpecificationLoader getSpecificationLoader() { + return IsisContext.getSpecificationLoader(); + } + + @Override + public IModel<String> getTitleLabel() { + return Model.of("Map"); + } + + @Override + public IModel<String> getCssClass() { + return Model.of("fa fa-map-marker"); + } +} diff --git a/fixture/pom.xml b/fixture/pom.xml index b0f69f4..beecb97 100644 --- a/fixture/pom.xml +++ b/fixture/pom.xml @@ -67,6 +67,12 @@ language governing permissions and limitations under the License. <artifactId>objenesis</artifactId> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.apache.poi</groupId> + <artifactId>poi</artifactId> + <version>3.12</version> + </dependency> </dependencies> diff --git a/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItem.java b/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItem.java index db6174d..257bf5e 100644 --- a/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItem.java +++ b/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItem.java @@ -18,19 +18,30 @@ */ package org.isisaddons.wicket.gmap3.fixture.dom; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; + +import javax.jdo.annotations.Column; import javax.jdo.annotations.IdentityType; +import javax.jdo.annotations.NotPersistent; import javax.jdo.annotations.VersionStrategy; + import com.google.common.base.Objects; import com.google.common.base.Predicate; import com.google.common.collect.Ordering; + import org.isisaddons.wicket.gmap3.cpt.applib.Locatable; import org.isisaddons.wicket.gmap3.cpt.applib.Location; +import org.isisaddons.wicket.gmap3.cpt.applib.Routeable; import org.isisaddons.wicket.gmap3.cpt.service.LocationLookupService; +import org.wicketstuff.gmap.api.GPoint; import org.apache.isis.applib.DomainObjectContainer; import org.apache.isis.applib.annotation.BookmarkPolicy; import org.apache.isis.applib.annotation.CollectionLayout; @@ -41,6 +52,7 @@ import org.apache.isis.applib.annotation.MinLength; import org.apache.isis.applib.annotation.Optionality; import org.apache.isis.applib.annotation.ParameterLayout; +import org.apache.isis.applib.annotation.Programmatic; import org.apache.isis.applib.annotation.Property; import org.apache.isis.applib.annotation.PropertyLayout; import org.apache.isis.applib.annotation.RenderType; @@ -48,6 +60,11 @@ import org.apache.isis.applib.services.clock.ClockService; import org.apache.isis.applib.util.ObjectContracts; import org.apache.isis.applib.util.TitleBuffer; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; @javax.jdo.annotations.PersistenceCapable(identityType=IdentityType.DATASTORE) @javax.jdo.annotations.DatastoreIdentity( @@ -95,7 +112,7 @@ named = "ToDo Item", bookmarking = BookmarkPolicy.AS_ROOT ) -public class Gmap3WicketToDoItem implements Comparable<Gmap3WicketToDoItem>, Locatable { +public class Gmap3WicketToDoItem implements Comparable<Gmap3WicketToDoItem>, Locatable, Routeable { //region > identification in the UI @@ -387,6 +404,84 @@ public int compareTo(final Gmap3WicketToDoItem other) { @javax.inject.Inject private LocationLookupService locationLookupService; + @NotPersistent + private List<String> points = new ArrayList<String>(); + + @Override + public List<GPoint> getRoute() { + final List<GPoint> route = new ArrayList<GPoint>(); + for (String point : points) { + String[] s = point.split(";"); + route.add(s[0] != null && s[1] != null ? new GPoint(Float + .valueOf(s[1]), Float.valueOf(s[0])) : null); + } + return route; + } + + @Programmatic + public void loadingPoints() throws IOException { + List sheetData = new ArrayList(); + FileInputStream fis = null; + + try { + fis = new FileInputStream("route.xls"); + HSSFWorkbook workbook = new HSSFWorkbook(fis); + HSSFSheet sheet = workbook.getSheetAt(0); + Iterator rows = sheet.rowIterator(); + + while (rows.hasNext()) { + HSSFRow row = (HSSFRow) rows.next(); + Iterator cells = row.cellIterator(); + List data = new ArrayList(); + + while (cells.hasNext()) { + HSSFCell cell = (HSSFCell) cells.next(); + data.add(cell); + } + + sheetData.add(data); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (fis != null) { + fis.close(); + } + } + showExelData(sheetData); + } + + @Programmatic + private void showExelData(List sheetData) { + + for (int i = 4; i < sheetData.size(); i++) { + + String point = ""; + + List list = (List) sheetData.get(i); + + for (int j = 2; j < 4; j++) { + + Cell cell = (Cell) list.get(j); + + if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { + + point = point + String.valueOf(cell.getNumericCellValue()); + point = point + ";"; + + } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { + + point = point + cell.getRichStringCellValue(); + point = point + ";"; + + } + } + + points.add(point); + System.out.println(point); + } + } //endregion } diff --git a/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItems.java b/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItems.java index 0390275..bb93dfd 100644 --- a/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItems.java +++ b/fixture/src/main/java/org/isisaddons/wicket/gmap3/fixture/dom/Gmap3WicketToDoItems.java @@ -18,8 +18,10 @@ */ package org.isisaddons.wicket.gmap3.fixture.dom; +import java.io.IOException; import java.util.Collections; import java.util.List; + import org.isisaddons.wicket.gmap3.cpt.applib.Location; import org.apache.isis.applib.DomainObjectContainer; import org.apache.isis.applib.annotation.Action; @@ -163,6 +165,12 @@ public Gmap3WicketToDoItem newToDo( toDoItem.setLocation( new Location(51.5172+random(-0.05, +0.05), 0.1182 + random(-0.05, +0.05))); + try { + toDoItem.loadingPoints(); + } catch (IOException e) { + e.printStackTrace(); + } + container.persist(toDoItem); container.flush();
route.xls
Description: MS-Excel spreadsheet