I am unable to get a route displayed on a Osmdroid map displayed on an Android 4.3 smartphone.
I am using osmbonuspack v4.4 jar and osmdroid-android v4.1 jar. I have overriden/copied OSRMRoadManager to CarRouteManager so that it makes use of the java.net.HttpURLConnection instead of the Apache HTTPConnection. With Apache HTTPConnection I don't get any route information back but with the java.net.HttpURLConnection I get route information back. The road has a mStatus of STATUS_OK and I can see the various parts of the route when I debug my code.
The route drawing function is the following:
public void DrawDirection(Road road)
{
if (road.mStatus == Road.STATUS_OK)
{
current_route = CarRouteManager.buildRoadOverlay(road, Color.BLUE, 3, this);
current_route.addPoint(user_point);
current_route.addPoint(clicked_location);
map_view.getOverlays().add(current_route);
map_view.invalidate();
}
}
With the above code I just get two polylines displayed on the map, one which traces straight from the GeoPoint user_point(starting point) to the GeoPoint clicked_location (destination). The other polyline traces to the right-bottom of the screen and off the current map area that is visible, when I zoom out to the world map it extends all the way off the edge of the map at Antartica.
When I remove the two lines
current_route.addPoint(user_point);
and
current_route.addPoint(clicked_location);
then there are no polylines drawn on the map. I have debugged the contents of current_route (which is of type PathOverlay) and the points in the mPoints array are valid GeoPoints, i.e. not null. When I map these GeoPoints onto the Google map at https://maps.google.com/ then it shows that these GeoPoints are valid locations on the roads/route between GeoPoint user_point(starting point) and GeoPoint clicked_location (destination).
Has anybody suffered the same affect and managed to solve it?
I would recommend you to:
get rid of your CarRouteManager stuff
just follow closely the OSMBonusPack tutorial
taking care about the "Important Note" at the very beginning of the Tutorials.
If you still have issues, have a look here:
RoadManager for osmdroid error
Only when you will have a better understanding of the inner behaviour of the lib, you will be able to implement your own RouteManager (if really needed).
Related
I was just wondering as to how to draw route direction(two point) of Google Maps in order for it to work offline. We have already downloaded offline Google Maps and then want navigation but do not know how to.
I was thinking of creating a navigation system with offline Google Maps, but I don't know how to draw route direction offline Google Map to work offline then embed it within my own application.
I have already used #mapbox Sdk, but my issue was I have downloaded offline location in Google Maps, after this location search any direction used two point direction in map, so I can drawline easily.
Using this : https://www.mapbox.com/android-sdk/examples/offline-manager/
Please help me on this one..
Could you clarify how exactly you are getting Google Directions API to work offline, to my knowledge the API only works online? Drawing the route can be done in a few different ways. The simpliest would be to convert the linestring the directions API gives you into multiple positions and then feed them into the polyline:
private void drawRouteLine(DirectionsRoute route) {
List<Position> positions = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6).getCoordinates();
List<LatLng> latLngs = new ArrayList<>();
for (Position position : positions) {
latLngs.add(new LatLng(position.getLatitude(), position.getLongitude()));
}
routeLine = mapboxMap.addPolyline(new PolylineOptions()
.addAll(latLngs)
.color(Color.parseColor("#56b881"))
.width(5f));
}
I'm trying to develop an offline navigation app for Trekking.
The app shows to the user a list of nearby track/routes where he can go.
Examples:
-Walking to Etna Vulcan
-Walking in the wood.
I've several KML files provided to me by local guides, each one for a different track.
So, if i choose "Walking to Etna Vulcan", I will have a Map with the track that I've to follow to go the Etna Vulcan, with some markers of interesting point (Examples: Refuges, Monuments). This data is actually ready, in KML format.
So:
The user chooses the track from the list that I provide, then the phone shows the track on the map(from kml or equivalent) with the current user position.
I'm doing it with MapBox sdk (but i'm open to alternatives).
Actually, I've successfully downloaded an offline map of the zone (Sicily), but i'm note sure how to write my kml on the map.
I also imported a kml in Mapbox Studio, but i don't know how to download a MapBox Studio Map into my application.
Thank you for your time!
If Mapbox seems like a good alternative then you just need to study their Android SDK to see if and how drawing markers and coordinate tracks are supported.
For the tracks see https://www.mapbox.com/android-sdk/geojson/
The relevant (to you) part is right at the end of their example code:
LatLng[] pointsArray = points.toArray(new LatLng[points.size()]);
// Draw Points on MapView
mapboxMap.addPolyline(new PolylineOptions()
.add(pointsArray)
.color(Color.parseColor("#3bb2d0"))
.width(2));
Here they have the coordinate points in the points ArrayList. They have been extracted from GeoJSON data, but you would just need to extract the coordinate points from the KML data and then draw the track based on that example.
For markers see https://www.mapbox.com/android-sdk/marker/
They have this example code:
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(48.13863, 11.57603))
.title("Hello World!")
.snippet("Welcome to my marker."));
For a marker with a custom icon see https://www.mapbox.com/android-sdk/custom-marker-icon/
They have this example code:
// Create an Icon object for the marker to use
IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
Drawable iconDrawable = ContextCompat.getDrawable(MainActivity.this, drawable.ic_directions_boat_black_18dp);
Icon icon = iconFactory.fromDrawable(iconDrawable);
// Add the custom icon marker to the map
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(-33.8500000, 18.4158234))
.title("Cape Town Harbour")
.icon(icon));
So you would just use a regular Android Drawable for the custom icon.
Looks quite convenient. I haven't used the Mapbox SDK myself.
I have a specific problem with Google Maps for Android. I should be able to make Google Maps look like the map on this page www.bam.brno.cz, but I'm new to Goole Maps for Android so I don't know if there's a way to do it.
I don't have any of those map images, but i guess there should be a way to get it from the internet (like WMS or something). If I was eventually able to save them and use them offline that would be great, but if not and the app would have to use data connection, it's also ok.
I already got app with google maps, so the question is just about how to get the map images and replace default google maps images.
Also if that wouldn't be posible, I would like to know what other options I have(like if theres some other map images I can use and how)
PS: not sure how it's called correctly: map images/map tiles
EDIT: I don't care about the objects, I know how to do them. I don't know how to get the whole map, the tiles it's made from. I'm already familiar with markers and camera moving etc.
Yes you can use marker for same.
You can try some thing like ..
m = myMap.addMarker(new MarkerOptions()
.position(new LatLng(startLatLng.latitude, startLatLng.longitude))
.title("Vivek Test"));
Here m is Marker;
and myMap is GoogleMap
Complete structure like this ..
myMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
//myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
myMap.setMyLocationEnabled(true);
myMap.moveCamera(CameraUpdateFactory.newLatLng(startLatLng));
myMap.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);
m = myMap.addMarker(new MarkerOptions()
.position(new LatLng(startLatLng.latitude, startLatLng.longitude))
.title("Vivek Test"));
Hope it helps. Cheers!
There is at least one file holding a series of coordinates (Latitude, Longitude) of each of the colored lines. Get those coordinates and use Polylines. These should take care of the colored lines. The grey dots would be Markers and these could be in the same file. Assuming that GoogleMaps has similar places names, then with these two you ought to be able to reproduce the map.
I would like to click a spot on a Google maps v2 android map. If the clicked point intersects a point on a polyline path, then display the polyline. I do not see any documented clickable events for polylines in android. I tried to extend the current Polyline object (marked final)
What other options do I have?
You can use library:
https://github.com/googlemaps/android-maps-utils
And detect clicks to polyline using next method (in OnMapClickListener):
PolyUtil.isLocationOnPath(point, polyline.getPoints(), isGeodesic, tolerance);
With the recent update of the maps api, v8.4, introduces clickable Polyline
As mentioned in the doc:
Use the OnPolylineClickListener to listen to click events on a
clickable polyline. To set this listener on the map, call
googleMap.setOnPolylineClickListener(...). When
a user clicks on a polyline, you will receive an
onPolylineClick(Polyline) callback.
gradle-dependency:
compile 'com.google.android.gms:play-services-maps:8.4.0'
implement callback: GoogleMap.OnPolylineClickListener
initialize Polyline:
Polyline polyline = googleMap.addPolyline(options);
polyline.setClickable(true);
...
receive events
#Override
public void onPolylineClick(Polyline polyline) {
....
}
Happy coding :)
Register an OnMapClickListener. Determine if a given click is on your line yourself. If it is, do whatever it was you wanted to do in this case.
I had a similar issue where I could not process click events on polylines. I was using Xamarin for Android which is C# but the functionality is largely the same as the Android Java Libraries in this case.
In the end, I ended up doing what seemed to be the only option.
This involved processing all of the midpoints of my polylines(of which there were around 1300). On every OnMapClick, I took the LatLng of the click event and performed a distance formula between it and the midpoint of all polylines in the static List<PolylineOptions>. I then attached a map marker to the closest polyline.
From a tap on a polyline, it pops up a marker in about a quarter of a second.
I imagine the implemented marker click events from the Google Maps API work in a similar way.
Here is the for loop that handles finding the closest point to a click.
int i = 0;//create an indexer for the loop
double shortestDist = 100;//set an initial very large dist just to be safe
int myIndex = 0;//set variable that will store the running index of the closest point
foreach (PolylineOptions po in myPolylines) {
var thisDist = Distance (point, midPoint (po.Points [0].Latitude, po.Points [0].Longitude, po.Points [1].Latitude, po.Points [1].Longitude));//calculate distance between point and midpoint of polyline
if (thisDist < shortestDist) {
shortestDist = thisDist;//remember current shortest distance
myIndex = i;//set closest polyline index to current loop iteration
}
i++;
}
I know it isn't the prettiest code but it gets the job done. I didn't see a real answer to this anywhere on the internet so here it is. It could probably be made more efficient by calculating the midpoints beforehand and storing them in an equally sized list and then not having to call the midpoint formula for each polyline on every map click but it works really fast already.
EDIT
I do my testing on a galaxy s3 by the way, so I think it's not too inefficient.
If you are using com.google.android.gms:play-services-maps:8.4.0 then it includes polylines click listener
googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener()
{
#Override
public void onPolylineClick(Polyline polyline)
{
//do your work selected polyline
}
});
PolylineOptions line = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(line);
polyline.setClickable(true);
We're porting an app from the v1 Maps API to the v2 API, and having trouble with markers.
We need markers that point in a specific geographic direction. In V1, we could build the bitmap pointing in the right direction at draw time, but in V2 the marker bitmap can't be changed.
I'm not sure if the best approach is to destroy and re-build all our markers when the map is rotated (which sounds like a performance problem), or to try drawing them all ourselves. That could be via a TileOverlay or via a view of our own that we sat on top of the map.
I don't really like any of these approaches. Has anyone tried any of them ?
UPDATE:
I've tried drawing via a view of our own, but that was far too laggy when the map was dragged.
I'm now destroying & recreating the markers, but that is (as expected) a performance problem, taking ~2000mS to update 60 markers.
Good news everyone! Google has added rotation to the Maps API, so we don't have to roll our own implementations anymore.
They have also added flat markers, which I guess is more related to the original question. A flattened marker will always stay in the orientation it was originally drawn on the map: https://developers.google.com/maps/documentation/android/marker#flatten_a_marker
The only requirement is that you reference the latest version of Google Play Services.
I'm also rewriting my app (Runbot) for the new API and had to figure out how to create custom markers representing milestones (like 1km, 2km, ...) and how to show or show not all of them depending on the zoom level.
I had a custom drawable that I used for the v1 API and what I do now to render the markers is about this (Position is a class of my own that holds the position and further information; all needed here is its LatLng property):
private void addMarker(Position p, MilestoneDrawable milestone) {
if (mMarkers.containsKey(p)) {
mMarkers.get(p).setVisible(true);
} else {
Marker m = mMap.addMarker(new MarkerOptions()
.position(p.latLng)
.icon(BitmapDescriptorFactory.fromBitmap(Util.drawableToBitmap(milestone)))
.anchor(0.5f, 1.0f) // bottom center
);
mMarkers.put(p, m);
}
}
Besides creating and adding the custom markers, what you see is that I keep the markers in a HashMap so I do not have to destroy and create them all the time. When it comes to zooming and deciding which ones to show, I first set all of the markers to invisible and than call addMarker() for those I want to be shown, and those which I already have in the HashMap I simply make visible again.
I hope this helps you a bit. I have a bit of mixed feelings towards the new API...
I had a similar problem where I had markers that needed to rotate. My solution was to have the object the marker represented be responsible for generating the marker. I have a few methods in the object that look like:
protected Marker getMarker(GoogleMap map) {
if (this.marker == null) {
marker = map.addMarker(new MarkerOptions().position(location).
icon(BitmapDescriptorFactory.fromBitmap(BusMarkerImageFactory.
getMarkerIcon(heading))));
}
return marker;
}
protected void updateMarker(GoogleMap map) {
if (marker != null) {
rotateIcon();
marker.setPosition(location);
} else {
getMarker(map);
}
private void rotateIcon() {
marker.setIcon(BitmapDescriptorFactory.
fromBitmap(BusMarkerImageFactory.getMarkerIcon(heading)));
}
This is from a system that draws buses with the markers pointing in the direction they are heading, so of course, your code will be different, but the concept is very similar. Instead of rebuilding the entire marker you're keeping a reference to it somewhere and then simply resetting the icon.
Of course, drawing all those bitmaps for minor changes is a drain on memory. I used a flyweight pattern in the (incorrectly named) BusMarkerImageFactory to keep 16 images for 16 possible heading ranges. It is a static class that simply takes in the heading and returns the image that I've mapped to that range.
can't you use addMarker(new MarkerOptions()) method ?
If you need a custom marker you can create an implementation of InfoWindowAdapter and use that implementation like mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
here is the documentation for InfoWindowAdapter