MarkerLayer and GeometryLayer onclick listener.
Hi Dev Team... I am having problems with the onClickListener if I combine MarkerLayer and GeometryLayer. Even though I clicked on the marker I created, Still the polygon is selected.... Tried adding geo layer first then marker layer and vice versa and still getting the same results....
mapView.getLayers().addLayer(geomLayer);
mapView.getLayers().addLayer(markerLayer);
GeometryLayer is always selected even when I click anywhere on the map....
Any suggestions?
What is your device exactly?
Lg optimus G running custom rom. 4.3.1
also tried it on nexus 7 version 4.2.2 still got the same results
Nexus 7 is with standard software? Can you post your code and data sample?
setContentView(R.layout.main);
// enable logging for troubleshooting - optional
Log.enableAll();
Log.setTag("hellomap");
// 1. Get the MapView from the Layout xml - mandatory
mapView = (MapView) findViewById(R.id.mapView);
// Optional, but very useful: restore map state during device rotation,
// it is saved in onRetainNonConfigurationInstance() below
Components retainObject = (Components) getLastNonConfigurationInstance();
if (retainObject != null) {
// just restore configuration and update listener, skip other
// initializations
mapView.setComponents(retainObject);
MyLocationMapEventListener mapListener = (MyLocationMapEventListener) mapView
.getOptions().getMapListener();
mapListener.reset(this, mapView);
mapView.startMapping();
return;
} else {
// 2. create and set MapView components - mandatory
mapView.setComponents(new Components());
}
// 3. Define map layer for basemap - mandatory.
// Here we use MapQuest open tiles
// Almost all online tiled maps use EPSG3857 projection.
TMSMapLayer mapLayer = new TMSMapLayer(new EPSG3857(), 0, 18, 0,
"http://10.0.13.7/leaflet/ASDYUIWNLFKSLAD/", "/", ".jpg");
mapView.getLayers().setBaseLayer(mapLayer);
adjustMapDpi();
// mapView.getOptions().setFPSIndicator(true);
// mapView.getOptions().setRasterTaskPoolSize(4);
// set initial map view camera - optional. "World view" is default
// Location: San Francisco
// NB! it must be in base layer projection (EPSG3857), so we convert it
// from lat and long
mapView.setFocusPoint(mapView.getLayers().getBaseLayer()
.getProjection().fromWgs84(-122.41666666667f, 37.76666666666f));
// rotation - 0 = north-up
mapView.setRotation(0f);
// zoom - 0 = world, like on most web maps
mapView.setZoom(16.0f);
// tilt means perspective view. Default is 90 degrees for "normal" 2D
// map view, minimum allowed is 30 degrees.
mapView.setTilt(65.0f);
// Activate some mapview options to make it smoother - optional
mapView.getOptions().setPreloading(true);
mapView.getOptions().setSeamlessHorizontalPan(true);
mapView.getOptions().setTileFading(true);
mapView.getOptions().setKineticPanning(true);
mapView.getOptions().setDoubleClickZoomIn(true);
mapView.getOptions().setDualClickZoomOut(true);
// set sky bitmap - optional, default - white
mapView.getOptions().setSkyDrawMode(Options.DRAW_BITMAP);
mapView.getOptions().setSkyOffset(4.86f);
mapView.getOptions().setSkyBitmap(
UnscaledBitmapLoader.decodeResource(getResources(),
R.drawable.sky_small));
// Map background, visible if no map tiles loaded - optional, default -
// white
mapView.getOptions().setBackgroundPlaneDrawMode(Options.DRAW_BITMAP);
mapView.getOptions().setBackgroundPlaneBitmap(
UnscaledBitmapLoader.decodeResource(getResources(),
R.drawable.background_plane));
mapView.getOptions().setClearColor(Color.WHITE);
// configure texture caching - optional, suggested
mapView.getOptions().setTextureMemoryCacheSize(40 * 1024 * 1024);
mapView.getOptions().setCompressedMemoryCacheSize(8 * 1024 * 1024);
// define online map persistent caching - optional, suggested. Default -
// no caching
// mapView.getOptions().setPersistentCachePath(this.getDatabasePath("mapcache").getPath());
// set persistent raster cache limit to 100MB
mapView.getOptions().setPersistentCacheSize(100 * 1024 * 1024);
// 5. Add simple marker to map.
// define marker style (image, size, color)
Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(
getResources(), R.drawable.olmarker);
MarkerStyle markerStyle = MarkerStyle.builder().setBitmap(pointMarker)
.setSize(0.5f).setColor(Color.WHITE).build();
// define label what is shown when you click on marker
Label markerLabel = new DefaultLabel("Ortigas MRT Station",
"Here is a marker");
// define location of the marker, it must be converted to base map
// coordinate system
MapPos markerLocation = mapLayer.getProjection().fromWgs84(121.0567f,
14.5879f);
// create layer and add object to the layer, finally add layer to the
// map.
// All overlay layers must be same projection as base layer, so we reuse
// it
MarkerLayer markerLayer = new MarkerLayer(mapLayer.getProjection());
markerLayer.add(new Marker(markerLocation, markerLabel, markerStyle,
null));
// add event listener
MyLocationMapEventListener mapListener = new MyLocationMapEventListener(
this, mapView);
mapView.getOptions().setMapListener(mapListener);
double[][] coords = { { 121.05929, 14.59219 }, { 121.06082, 14.59087 },
{ 121.06063, 14.59066 }, { 121.0604, 14.59084 },
{ 121.05981, 14.59018 }, { 121.05833, 14.59141 },
{ 121.05833, 14.59152 }, { 121.05841, 14.59161 },
{ 121.05849, 14.59162 }, { 121.05871, 14.59188 },
{ 121.05883, 14.59188 }, { 121.05893, 14.59181 },
{ 121.05929, 14.59219 } };
double[][] coords1 = { { 121.058446, 14.591231 },
{ 121.058601, 14.591251 }, { 121.058623, 14.591282 },
{ 121.058617, 14.591571 }, { 121.058625, 14.591433 },
{ 121.058596, 14.591481 }, { 121.058432, 14.591451 },
{ 121.058411, 14.591402 }, { 121.058406, 14.591264 } };
ArrayList<MapPos> mapPoses = new ArrayList<MapPos>();
ArrayList<MapPos> mapPoses1 = new ArrayList<MapPos>();
for (double[] coord : coords) {
mapPoses.add(mapLayer.getProjection().fromWgs84((float) coord[0],
(float) coord[1]));
}
for (double[] coord1 : coords1) {
mapPoses1.add(mapLayer.getProjection().fromWgs84((float) coord1[0],
(float) coord1[1]));
}
// now define style for the buildings. Usually just color
Polygon3DStyle polygon3DbuildingStyle = Polygon3DStyle.builder()
.setColor(Color.GREEN | 0x40ffffff).build();
// now you can make real building object. Height is here empirically
// found value which gives nice result.
Polygon3D building3D = new Polygon3D(mapPoses, null, 0.5f,
new DefaultLabel("Robinson's Galleria"),
polygon3DbuildingStyle, null);
Polygon3D building3D1 = new Polygon3D(mapPoses1, null, 4f,
new DefaultLabel("HanCole Corporation"),
polygon3DbuildingStyle, null);
// create layer and add object to it, add layer to map
Polygon3DLayer polygon3DLayer = new Polygon3DLayer(
mapLayer.getProjection());
polygon3DLayer.add(building3D);
polygon3DLayer.add(building3D1);
// mapView.getLayers().addLayer(polygon3DLayer);
int minZoom = 10;
// load bitmaps for vector elements. You can get the images from Hellomap3D project res/drawable
// these are simple anti-aliased bitmaps which can change colour, should be good for most cases
Bitmap lineMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.line);
// set styles for all 3 object types: point, line and polygon
StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
PointStyle pointStyle = PointStyle.builder().setBitmap(pointMarker).setSize(0.1f).setColor(Color.GREEN).build();
pointStyleSet.setZoomStyle(minZoom,pointStyle);
// We reuse here pointStyle for Line. This is used for line caps, useful for nicer polylines
// Also do not forget to set Bitmap for Line. This allows to have fancy styles for lines.
StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>();
lineStyleSet.setZoomStyle(minZoom, LineStyle.builder().setBitmap(lineMarker).setWidth(0.1f).setColor(Color.GREEN).setPointStyle(pointStyle).build());
PolygonStyle polygonStyle = PolygonStyle.builder().setColor(Color.BLUE).build();
StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>(null);
polygonStyleSet.setZoomStyle(minZoom, polygonStyle);
Projection proj = mapView.getLayers().getBaseProjection();
// add polygon with a hole. Inner hole coordinates must be entirely
// within.
double[][] pCoordsOuter = { { 121.05929, 14.59219 },
{ 121.06082, 14.59087 }, { 121.06063, 14.59066 },
{ 121.0604, 14.59084 }, { 121.05981, 14.59018 },
{ 121.05833, 14.59141 }, { 121.05833, 14.59152 },
{ 121.05841, 14.59161 }, { 121.05849, 14.59162 },
{ 121.05871, 14.59188 }, { 121.05883, 14.59188 },
{ 121.05893, 14.59181 }, { 121.05929, 14.59219 } }; // outer
// ring
double[][] pCoordsInner = { { 1, 10 }, { 1, 50 }, { 10, 50 }, { 1, 10 } }; // inner
// ring
ArrayList<MapPos> outerPoses = new ArrayList<MapPos>();
for (double[] coord : pCoordsOuter) {
outerPoses.add(proj.fromWgs84((float) coord[0], (float) coord[1]));
}
ArrayList<MapPos> innerPoses = new ArrayList<MapPos>();
for (double[] coord : pCoordsInner) {
innerPoses.add(proj.fromWgs84((float) coord[0], (float) coord[1]));
}
// we need to create List of holes, as one polygon can have several
// holes
// here we have just one. You can have nil there also.
// List<List<MapPos>> holes = new ArrayList<List<MapPos>>(); // holes.add(innerPoses); GeometryLayer geomLayer = new GeometryLayer(proj); geomLayer.add(new Polygon(outerPoses, null, new DefaultLabel("Polygon"), polygonStyleSet, null)); mapView.getLayers().addLayer(geomLayer); mapView.getLayers().addLayer(markerLayer); }
yup.. nexus 7 is running on official android 4.3
I just tested the provided sample code with Nexus 7 (Android 4.2.2) and could not reproduce the issue, everything worked as expected (used SDKs 2.2.1 and 2.3.0). Can you send us full sample of your app?