mapping MapView touch events to GeoCoordinates

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

mapping MapView touch events to GeoCoordinates

Postby ptucker » Wed Jun 10, 2009 5:06 pm

I'm having trouble with the Google Maps API, specifically handling
touch events in MapView and mapping those events to Geo coordinates.

Requirement:
I've created a MapActivity containing a MapView. Drag and zoom work
fine, and I've successfully added an ItemizedOverlay that displays and
updates properly. Right now I can add OverlayItems to the map center
via a button, but I would like to add functionality allowing the user
to issue a long click to add an OverlayItem to the map at the selected
location.

It seems like there should be an easy way to determine the geo
location for a click event in MapView, but from what I've gathered it
requires writing my own pixel->latlong translation based on the view
span. I scavenged snippet from anddev.org and tweaked it to work
properly to translate from screen pixel to GeoPoint:

private GeoPoint pixelToGeoPoint(int selectedPixelX, int
selectedPixelY) {
Log.v(LOG_ID, "selectedPixel=[" + selectedPixelX + "," +
selectedPixelY + "]");

// center point in udegrees
GeoPoint center = mapView.getMapCenter();
int centerUdegreesX = center.getLongitudeE6();
int centerUdegreesY = center.getLatitudeE6();
Log.v(LOG_ID, "centerUdegrees=[" + centerUdegreesX + "," +
centerUdegreesY + "]");

// dimension of view area in udegrees
int viewUdegreesX = mapView.getLongitudeSpan();
int viewUdegreesY = mapView.getLatitudeSpan();
Log.v(LOG_ID, "viewUdegrees=[" + viewUdegreesX + "," +
viewUdegreesY + "]");

// dimension of view area in pixels
int viewPixelsX = mapView.getWidth();
int viewPixelsY = mapView.getHeight();
Log.v(LOG_ID, "viewPixels=[" + viewPixelsX + "," + viewPixelsY +
"]");

// center point in pixels
int centerPixelX = viewPixelsX / 2;
int centerPixelY = viewPixelsY / 2;
Log.v(LOG_ID, "centerPixel=[" + centerPixelX + "," + centerPixelY
+ "]");

// udegrees per pixel
int udegreesPerPixelX = viewUdegreesX / viewPixelsY;
int udegreesPerPixelY = viewUdegreesY / viewPixelsX;
Log.v(LOG_ID, "udegreesPerPixel=[" + udegreesPerPixelX + "," +
udegreesPerPixelY + "]");

// delta from center of selection, in pixels
int deltaPixelX = selectedPixelX - centerPixelX;
int deltaPixelY = selectedPixelY - centerPixelY;
Log.v(LOG_ID, "deltaPixel=[" + deltaPixelX + "," + deltaPixelY +
"]");

// delta from center of selection, in udegrees
int deltaUdegreesX = udegreesPerPixelX * deltaPixelX;
int deltaUdegreesY = -udegreesPerPixelY * deltaPixelY;
Log.v(LOG_ID, "deltaUdegrees=[" + deltaUdegreesX + "," +
deltaUdegreesY + "]");

// selection in udegrees
int selectedUdegreesX = centerUdegreesX + deltaUdegreesX;
int selectedUdegreesY = centerUdegreesY + deltaUdegreesY;
Log.v(LOG_ID, "selectedUdegrees=[" + selectedUdegreesX + "," +
selectedUdegreesY + "]");

GeoPoint selectedPoint = new GeoPoint(selectedUdegreesY,
selectedUdegreesX);
return selectedPoint;
}

This sort of works, but it breaks down with the Mercator projection.
ie, latitutes are father apart the father form the equator you are.
Also, if you zoom out far enough, you can go off the top or bottom of
the map. In this case, mapView.getMapCenter() returns a point off the
map (i.e., > 90 or < -90 degrees), but mapView.getLatitudeSpan()
returns the height of only the viewable area. So, it throws the math
off. And I can't figure out a way to grab either the center of the
displayed map section, or the height in microdegrees of the entire
viewable area. I can't even find anything in the API to return any
indication that part of the view is off the map.
ptucker
Freshman
Freshman
 
Posts: 4
Joined: Wed Jun 10, 2009 5:05 pm

Top

Re: mapping MapView touch events to GeoCoordinates

Postby ptucker » Sat Jun 13, 2009 5:07 am

Aha! MapView.getProjection answers all.
ptucker
Freshman
Freshman
 
Posts: 4
Joined: Wed Jun 10, 2009 5:05 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 1 guest