MapView latitudeSpan / longtitudeSpan not working - android

I am trying to get the bounds of my mapview but I had the problem that the latitude is always set to 0 and longtitude is always set to 360*1E6. According to this link, that is because it will only offer the right coordinates when the map is fully loaded:
Mapview getLatitudeSpan and getLongitudeSpan not working
Now, I am totally confused about the solution, I made this method which I call from the onCreate of my mainactivity (the mapview):
public int[][] getBounds()
{
GeoPoint center = this.mapView.getMapCenter();
int latitudeSpan = this.mapView.getLatitudeSpan();
int longtitudeSpan = this.mapView.getLongitudeSpan();
int[][] bounds = new int[2][2];
bounds[0][0] = center.getLatitudeE6() + (latitudeSpan/2);
bounds[0][1] = center.getLongitudeE6() + (longtitudeSpan/2);
bounds[1][0] = center.getLatitudeE6() - (latitudeSpan/2);
bounds[1][1] = center.getLongitudeE6() - (longtitudeSpan/2);
return bounds;
}
How do I make this wait for the mapview to load? I've looked in the API for postDelayed, but I cannot get it to work.
Forgive me if I am being stupid o.o'

The correct way to create a "Timer" in android is to utilize android.os.Handler:
private Handler updateHandler = new Handler();
updateHandler.postDelayed(waitForMapTimeTask, TIME_TO_WAIT_IN_MS);
private Runnable waitForMapTimeTask = new Runnable() {
public void run() {
getBounds();
// Read the bounds to see if something reasonable is returned
if (!mapIsLoaded) {
updateHandler.postDelayed(waitForMapTimeTask, TIME_TO_WAIT_IN_MS);
}
}
};

Related

Xamarin.Android Google Maps is blured on navigation

I am building a car navigation application in Xamarin.Android (native not forms). I had read a lot, and follow instructions I found on how to add the map in the application and also how to use the location services, either with the FusedLocationProvider or the NavigationManager. I decided to use the NavigationManager since it looks more accurate because of the number of decimal digits (14) in the LatLng. All good up to now. I am testing my app and while the marker and the map are moving, in order to follow the car's direction, the map is blurred (see the attached pictures), it looks like it does not have the appropriate time to get refreshed.
What I am doing in order to move the camera and the marker?
1. OnLocationChanged from the LocationManager I am calling the UpdateCameraBearing function
internal static void UpdateCameraBearing(float bearing, Location newLocation)
{
if (Map == null)
return;
// Save current zoom
int originalZoom = (int)Map.CameraPosition.Zoom;
if (!startingZoom.HasValue)
{
startingZoom = originalZoom = MapZoomDef;
}
LastLocation = newLocation;
LatLng newPosition = new LatLng(newLocation.Latitude, newLocation.Longitude);
CameraPosition camPos = new CameraPosition(newPosition, originalZoom != MapZoomDef ? originalZoom : MapZoomDef, 0, bearing);
try
{
Map.AnimateCamera(CameraUpdateFactory.NewCameraPosition(camPos));
UpdateMarkerPosition(newPosition);
}
catch (Exception ex)
{
Log.Error(TAG, ex.Message);
}
}
This function animates the camera to the new position and also updates the marker position using the UpdateMarkerPosition function as follows
private static void UpdateMarkerPosition(LatLng newPosition)
{
if (null == MapMarker)
{
SetNewMarker(newPosition);
}
else
{
//positionMarker.Visible = true;
//positionMarker.Position = newPosition;
AnimateMarker(newPosition, new LatLngInterpolatorSpherical());
}
}
private static void SetNewMarker(LatLng myPosition)
{
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.SetPosition(myPosition);
markerOptions.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Mipmap.ic_car));
MapMarker = Map.AddMarker(markerOptions);
}
private static void AnimateMarker(LatLng finalPosition, LatLngInterpolatorSpherical latLngInterpolator)
{
Android.OS.Handler handler = new Android.OS.Handler();
long start = Android.OS.SystemClock.UptimeMillis();
LatLng startPosition = MapMarker.Position;
Android.Views.Animations.AccelerateDecelerateInterpolator interpolator = new Android.Views.Animations.AccelerateDecelerateInterpolator();
float durationInMs = 1000;
Java.Lang.Runnable mUpdateGeneration = null;
mUpdateGeneration = new Java.Lang.Runnable(() => {
long elapsed;
float t;
float v;
// Calculate progress using interpolator
elapsed = Android.OS.SystemClock.UptimeMillis() - start;
t = elapsed / durationInMs;
v = interpolator.GetInterpolation(t);
MapMarker.Position = latLngInterpolator.Interpolate(v, startPosition, finalPosition);
// Repeat till progress is complete.
if (t < 1)
{
// Post again 16ms later.
handler.PostDelayed(mUpdateGeneration, 16);
}
});
handler.Post(mUpdateGeneration);
}
The UpdateMarkerPosition function checks if the marker exists and if not (this is the first time) it creates it, otherwise it calls the AnimateMarker function where I am using an Interpolator in order to simulate a smooth marker movement.
Last but not least the LocationManager.RequestLocationUpdates is configured with 1-second minimum time and 1-meter minimum Distance.
I will appreciate any suggestions on how to solve the problem...
enter image description here
enter image description here

How to set bounding box in mapsforge just like in osmdroid and how do I put a text above or below the pathLayer?

How to set bounding box in mapsforge just like in osmdroid and how do I put a text above or below the pathLayer?
In osmdroid, I usually call the setScrollableAreaLimit() method but in mapsforge there's no such method in the mapView. How do I accomplish this?
And also how do I add a TextOverlay below or above the PathLayer?
//Bounding Box
maxScrollableLimit = new BoundingBox(14.7882,121.1421,14.3469,120.8990);
...
private PathLayer createPathLayerFirst(PathWrapper response) {
Style style = Style.builder()
.generalization(Style.GENERALIZATION_SMALL)
.strokeColor(0x9900cc33)
.strokeWidth(4 * getResources().getDisplayMetrics().density)
.build();
PathLayer pathLayer = new PathLayer(mapView.map(), style);
List<GeoPoint> geoPoints = new ArrayList<>();
PointList pointList = response.getPoints();
for (int i = 0; i < pointList.getSize(); i++)
geoPoints.add(new GeoPoint(pointList.getLatitude(i), pointList.getLongitude(i)));
pathLayer.setPoints(geoPoints);
return pathLayer;
}
From the code you provide, it is not clear if you looked at the mapsforge docs at all. Do so first, then return to my answer.
For setting map limits, add the following code to your onStart() method:
mapView.getModel().mapViewPosition.setMapLimit(this.getBoundingBox());
And in the same class, have the following method ready:
private BoundingBox getBoundingBox() {
final double MINLAT = Double.valueOf(res.getString(R.string.mapminlat));
final double MINLON = Double.valueOf(res.getString(R.string.mapminlon));
final double MAXLAT = Double.valueOf(res.getString(R.string.mapmaxlat));
final double MAXLON = Double.valueOf(res.getString(R.string.mapmaxlon));
return new BoundingBox(MINLAT, MINLON, MAXLAT, MAXLON);
}
For adding a layer to your mapview do the following:
mapView.getLayerManager().getLayers().add(layer);
For creating a layer, see the docs. Take a special look at the examples; maybe the LabelLayer is what you are looking for.

Zooming and panning to lat/lng span in map activity

Summary
I need to be able to move from my current mapview to a latitude/longitude span. I want this move to happen in an animated sort of way (similar to how animateTo works). I need to go from my current view, and zoom in or out to end up at the target latitude/longitude span. Below is the code I'm using to do this when I use zoomLevels. But I have to use spans, not zoom levels to accomplish this.
[Edit]
Here's what I'm doing for animateTo. I need the equivalent of this, but using Lat/Lng Span to end up at my final view (i need to be zoomed and centered, showing the lat/lng span provided). As you'll notice, i'm using targetZoomLevel and currentZoomLevel to determine how much zooming to do... I need to be using the lat/lng span instead.
public static void smoothZoom(final MapController controller,
final int currentZoomLevel, final int targetZoomLevel,
int latitude, int longitude) {
final Handler handler = new Handler();
controller.animateTo(new GeoPoint(latitude, longitude), new Runnable() {
public void run() {
int currentZoom = currentZoomLevel;
int zoomTarget = targetZoomLevel;
// Smooth zoom handler
// int zoomLevel = _mapView.getZoomLevel();
// int targetZoomLevel = targetLevel;
long delay = 0;
if (zoomTarget > currentZoom) {
while (currentZoom++ < zoomTarget) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
controller.zoomIn();
}
}, delay);
delay += 150; // Change this to whatever is good on the
// device
}
}
}
});
}
[Edit]
I really need this code for my application now, there is no way around it. First person to help me get working code gets 250 rep.
This has worked for me:
MapController.setCenter(GeoPoint);
You can get MapController from:
MapView.getController();
And get a GeoPoint from:
new GeoPoint( (int) ( LAT * 1e6 ) , (int) ( LONG * 1e6 ) );
Is this the kind of solution you're looking for?
public static void zoomSpan(final MapView mapView, int northernLat, int southernLat, int easternLon, int westernLon) {
final int latSpan = northernLat - southernLat;
final int lonSpan = easternLon - westernLon;
latCenter = southernLat + latSpan / 2;
lonCenter = westernLon + lonSpan / 2;
final MapController controller = mapView.getController();
final Handler handler = new Handler();
final Runnable zoomRunnable = new Runnable() {
public void run() {
int viewLatSpan = mapView.getLatitudeSpan();
int viewLonSpan = mapView.getLongitudeSpan();
boolean actionTaken = false;
if (mapView.getZoomLevel() > 1 && (latSpan > viewLatSpan || lonSpan > viewLonSpan)) {
controller.zoomOut();
actionTaken = true;
}
if (mapView.getZoomLevel() < mapView.getMaxZoomLevel() && latSpan * 2 <= viewLatSpan && lonSpan * 2 <= viewLonSpan) {
controller.zoomIn();
actionTaken = true;
}
if (actionTaken)
handler.post(zoomRunnable);
}
};
controller.animateTo(latCenter, lonCenter, zoomRunnable);
}
In my map application I needed to implement a location search feature. If location found it moves to that location, add an overlay and zooms to a predefined level.
I used following code for this purpose:
final int SEARCH_ZOOM_LEVEL = 7;
controller = mMapView.getController();
controller.setZoom(SEARCH_ZOOM_LEVEL );
pointToLocation(s); // s is the location
pointToLocation() was implements like this:
private void pointToLocation(String s) {
Geocoder gc = new Geocoder(MapDemo.this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocationName(
s, 5);
String add = "";
if (addresses.size() > 0) {
point = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(point);
}
} catch (IOException e) {
Toast.makeText(MapDemo.this, "Location not found", Toast.LENGTH_LONG).show();
}
}
The usage of the function you're looking at is zoomToSpan(int latSpanE6, int lonSpanE6).
You have the current span (or can get it easily) and the target span (as you said you know it), so why don't you simply start calling zoomToSpan on a delay , as you incrementally move latSpanE6 and longSpanE6 towards the target. It shouldn't be that difficult. You already have most of the code there, but instead of zoomIn while checking the zoom level, you would zoomToSpan while checking if latSpanE6 and longSpanE6 have reached your target span.
Won't bother coding it since you can do it yourself ;)

How to animate marker when it is added to map on Android?

I want to animate map markers when they are added to map.
User should see the map with markers around him. Each new marker should bounce.
You can implement the onMarkerClick() and make the marker bounce whenever user clicks on it.
Just try out below code implementation. I have tried it and it works totally fine.
private Marker mPerth;
private Marker mPerth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.title("Perth")
.snippet("Population: 1,738,800"));
#Override
public boolean onMarkerClick(final Marker marker)
{
// This causes the marker at Perth to bounce into position when it is clicked.
if (marker.equals(mPerth)) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mMap.getProjection();
Point startPoint = proj.toScreenLocation(PERTH);
startPoint.offset(0, -100);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 1500;
final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
double lng = t * PERTH.longitude + (1 - t) * startLatLng.longitude;
double lat = t * PERTH.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}
// We return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
You can also use this at the time of adding the marker in your application besides onClick event.
I hope this what you want only.
Anchor the marker off screen or at your start position then start the animation.
Note the .setAnchor used in this method was added to the google map api v2 in May 2013
I've just now got this working for one marker by tweaking the extras samples maps demo and I don't like the performance of this implementation. The most important piece is to anchor the marker off screen or off at your start position. I'm using off screen above.
Anchor the marker off screen .setAnchor(.5f,(size of screen above marker / size of marker )) //for the map demo perth is about 6f for my test phone. Change the animation to bounce to the same value it was 6f for my test phone.
private void addMarkersToMap() {
// A few more markers for good measure.
mPerth = mMap.addMarker(new MarkerOptions().position(PERTH)
.title("Perth").snippet("Population: 1,738,800")
.anchor(.5f, 6f)
);
Change the animation so it bounces to (size of screen above marker/size of marker) (6f on my test phone). I'm just using the onclick handler because it's already setup to bounce with tweaks bounce to 6f and a longer duration. So after all the markers have been added to the map I fire off the click handler.
this.onMarkerClick(mPerth);
The changed onMarkerClick handler with the 6f and longer duration.
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(mPerth)) {
// This causes the marker at Perth to bounce into position when it
// is clicked.
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 2500;
final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = Math.max(
1 - interpolator.getInterpolation((float) elapsed
/ duration), 0);
marker.setAnchor(0.5f, 1.0f + 6 * t);
if (t > 0.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
} else if (marker.equals(mAdelaide)) {
// This causes the marker at Adelaide to change color.
marker.setIcon(BitmapDescriptorFactory.defaultMarker(new Random()
.nextFloat() * 360));
}
// We return false to indicate that we have not consumed the event and
// that we wish
// for the default behavior to occur (which is for the camera to move
// such that the
// marker is centered and for the marker's info window to open, if it
// has one).
return false;
}
Good Luck
You can add any new layout to MapView as map marker:
public void AddAnimMarkerToMap(MapView map, GeoPoint geoPoint, int id, int animResId)
{
var layoutParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WrapContent,
ViewGroup.LayoutParams.WrapContent,
geoPoint,
MapView.LayoutParams.Center);
var ll = new LinearLayout(map.Context) { Id = id, Orientation = Orientation.Vertical };
ll.SetGravity(GravityFlags.Center);
var iv = new ImageView(map.Context);
iv.SetImageResource(animResId);
ll.AddView(iv);
map.AddView(ll, layoutParams);
var markerAnimation = (AnimationDrawable)iv.Drawable;
markerAnimation.Start();
ll.LayoutParameters = layoutParams;
}
probably you can add ImageView directly without wraping layout.
animResId is the Frame animation drawable resource (similar as Android Mylocation marker).
http://developer.android.com/guide/topics/resources/animation-resource.html#Frame

Animation at a specified rate using canvas / Ondraw

In my Android app, I am trying to show letters one by one with a short delay between each, while also playing a sound for each letter. I have everything working, and the sounds play with the correct delay, but the text always prints to the screen far too fast. The canvas seems to be updated even when i am not specifically invalidating the view.
Here is what I have so far - I also tried a variant of this based on the "snake" example and had the same results... any help would be appreciated!
public class SpellingView extends View {
private static final String WORD = "TRUCK";
int width;
int height;
String textToPrint;
float textspace;
int j=0;
private final Path arc;
private final Paint tPaint;
//constructor for SpellingView
public SpellingView(Context context) {
super(context);
arc = new Path();
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
displayLetterLoop();
}
public void displayLetterLoop(){
for (int i = 0; i < WORD.length(); i++){
final Runnable mUpdateUITimerTask = new Runnable() {
public void run() {
Spelling.mp.start();
}
};
final Handler mHandler = new Handler();
mHandler.postDelayed(mUpdateUITimerTask, i*1500);
}
}
#Override
protected void onDraw(Canvas canvas) {
int k;
// Drawing commands go here
width = canvas.getWidth();
height = canvas.getHeight();
arc.addArc(new RectF((width*.15f), (height*.15f), (width*.85f), (height*.4f)), 180,180);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
tPaint.setColor(Color.RED);
tPaint.setTextSize(height * 0.1f);
tPaint.setTextAlign(Paint.Align.LEFT);
setBackgroundColor(Color.BLACK);
for (k = 0; k < j; k++){
char c = WORD.charAt(k);
String cs = Character.toString(c);
textToPrint+= cs;
textspace =(float) (k*(width/WORD.length())*.9);
canvas.drawTextOnPath(cs, arc, textspace , 0, tPaint);
}
if(j<WORD.length()){
j++;
}
}
}
Custom view will invalidate itself when is a part of a layout which for some reason redraw itself. Therefore you could envelop your code in onDraw() with a condition and a flag so that it draws your stuff only when the timer sets the flag and calls invalidate. After one letter is drawn then the flag shoud be set on false like:
if (drawLetter){
drawLetter = false;
/code...
}
However this also may need to be a sychronized block.
OnDraw should happen 60 times a second and not only when you are invalidating.
So maybe you need to update some class variables (when you are invalidating) and use those for your draw logic # OnDraw.

Categories

Resources