Google map geoJson layer feature info on click - android

smart people :)!
Story: I have many layers what I have added to google maps in android. I am adding them by iterating - creating new GeoJsonLayer and calling method addLayerToMap(). I have created setOnFeatureClickListener on last GeoJsonLayer (Maybe here is the problem).
Problem: When I click on feature (LineString, Polygon) I receive information only about last Layer - polygon. Then I added mMap.setOnPolylineClickListener(), then click listener is going into correct methods - for LineString in PolyLineClick... etc, but here again is problem - how can I get information from Polyline about LineString properties, what I added to geoJson?
Question: What is the best approach to add layers to map , so I can manage click events and show LineString or Polygon properties for user?
Code example:
geoJsonLayer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
#Override
public void onFeatureClick(Feature feature) {
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
#Override
public void onPolylineClick(Polyline polyline) {
//TODO show LineString properties
//Comes here when LineString clicked
}
});
mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
#Override
public void onPolygonClick(Polygon polygon) {
//TODO show Polygon properties
//Comes here when Polygon clicked
}
});
}

Found solution - added all layers into 1 layer and everything works fine.

Related

Android get properties for polygon from GeoJson

Im adding GeoJsonLayer to Google map on my application. This json contains coordinates for polygon and properties for each polygon. What I need is to get those properties when polygon is clicked. This is the code Im using to achieve that.
GeoJsonLayer jsonLayer = new GeoJsonLayer(mMap, geoJSON);
jsonLayer.addLayerToMap();
jsonLayer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
#Override
public void onFeatureClick(final com.google.maps.android.data.Feature feature) {
mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
#Override
public void onPolygonClick(Polygon polygon) {
for (Object s : feature.getProperties()) {
Log.d("getProperties", "getProperties = " + s.toString());
}
}
});
}
});
The problem is that it returns all properties. I want it to return properties only for the polygon I clicked.
Also its possible to draw polygons with user input. When I click on those polygons it triggers onPolygonClick() inside jsonLayer.setOnFeatureClickListener(). How to check if clicked polygon is from jsonLayer?

Make graphics clickable instead of create new graphics in map

So, I have a map with a MapOnTouchListener on it. I have overriden the OnSingleTap method (allPoints is an array where all markers are added for a non related to this question functionality):
#Override
public boolean onSingleTap(MotionEvent point) {
Point mapPoint=view.toMapPoint(point.getX(), point.getY());
new GeocoderAsynckTask(mapPoint,view).execute(mapPoint);
SpatialReference sp = SpatialReference.create(SpatialReference.WKID_WGS84);
Point p1Done=(Point) GeometryEngine.project(mapPoint, mvHelper.getMapView().getSpatialReference(), sp);
allPoints.add(p1Done);
return super.onSingleTap(point);
}
That GeoCoderAsyncTask translates the coords of the point to an address, and adds a marker to the map. The geocoding is done just right, I get my address and in PostExecute method I add the marker to the map:
protected void onPostExecute(String address) {
mProgressDialog.dismiss();
Toast.makeText(ctx,address,Toast.LENGTH_LONG).show();
SpatialReference sp = SpatialReference.create(SpatialReference.WKID_WGS84);
Point p1Done=(Point) GeometryEngine.project(point, mvHelper.getMapView().getSpatialReference(), sp);
mvHelper.addMarkerGraphic(p1Done.getY(), p1Done.getX(), "Location", address, android.R.drawable.ic_menu_myplaces, null, false, 1);
}
The problem is, I want the markers to be clickable, but if I click them, another marker is added. I have tried to nullify the MapOnTouchListener, but that way, no marker is added, and no click on marker is detected...
How could I acomplish that?
Thank you.
EDIT:
if I simply set a SingleTapListener on the map at the start of the execution of the app instead of setting my MapOnTouchListener, the markers are created, and are clickable:
mMapView.setOnSingleTapListener(new OnSingleTapListener() {
#Override
public void onSingleTap(float v, float v1) {
mMapView.createIcon(v,v1);
}
});
After that, by clicking a button, I set the MapOnTouchListener, because I need another functionality (create an envelope and store the points in that envelope inside an array). After doing that, I try to nullify the MapOnTouchListener, and after that set the singleTapListener as in the start of the execution...and the markers are created, but cannot be clicked!!

Info Window on shapes(circle, polygon) in Android Maps App

I have an android app which one of its screens uses google maps, and I load some regions on it. I want to be able to click on a region/shape(circle or polygon) and an info window to appear. I am searching but I cant find enough information on how to show info windows on shapes, only on markers. Is that not a common practice? What should I do? Could anyone help, even theoretically? Any help or direction would be appreciated, thank you very much!
Create a customCircleOnClickListener (or don't and use the title and
snippet properties, as I am doing in step 3).
Then add:
mMap.setInfoWindowAdapter(new
CustomInfoWindowAdapter(MainActivity.this));
mMap.setOnCircleClickListener(customCircleOnClickListener());
Finally:
public GoogleMap.OnCircleClickListener customCircleOnClickListener() {
return new GoogleMap.OnCircleClickListener() {
#Override
public void onCircleClick(Circle circle) {
Log.d("MainActivity", "Clicked a ball!");
POIAnnotationCircle annotCircle = annotationsByCircle.get(circle);
markerForInfoWindow = mMap.addMarker(new MarkerOptions()
.alpha(0.0f)
.infoWindowAnchor(.6f,1.0f)
.position(annotCircle.getCoordinate())
.title(annotCircle.getTitle())
.snippet(annotCircle.getSnippet()));
markerForInfoWindow.showInfoWindow();
}
};
}

Android: find fastest route between 2 or more points depeding on streets in GoogleMaps

Am adding markers in my app on GoogleMaps,my markers allways are on streets.For example I have moore than 2 markers on map, I need to draw optimal route between them depending on streets.Is there any service to help me, or I need to create my own algorithm? Until now I worked a little with Google Elevation API and Google Direction API, but now I need Google Roads API, to find optimal roads between points? It must be something simmilar to my needs.
Consider using following library: https://github.com/akexorcist/Android-GoogleDirectionLibrary
GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
.from(new LatLng(37.7681994, -122.444538))
.to(new LatLng(37.7749003,-122.4034934))
.avoid(AvoidType.FERRIES)
.avoid(AvoidType.HIGHWAYS)
.execute(new DirectionCallback() {
#Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
// Do something
} else {
// Do something
}
}
#Override
public void onDirectionFailure(Throwable t) {
// Do something
}
});
Gradle
compile 'com.akexorcist:googledirectionlibrary:1.0.5'

touch the screen and add a marker in android

I want add a marker when I touch the screen. I use osmdroid and no google map.
Moreover, I want get back coordinate of this marker.
for the moment, I can display the osm map and display the location of the user.
thanks !
You can use MapEventsReceiver.
First make sure you have
implementation 'org.osmdroid:osmdroid-mapsforge:6.1.6'
in your build gradle (or newer version).
Then you make you OSMDroid class implement it
public class OSMapClass implements MapEventsReceiver {
// Your map implementation here
#Override
public boolean singleTapConfirmedHelper(GeoPoint p) {
// Here implement single click behaviour
return false;
}
#Override
public boolean longPressHelper(GeoPoint p) {
// Here implement long click behaviour
return false;
}
}

Categories

Resources