toPixels() doesn't seem to work properly

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

toPixels() doesn't seem to work properly

Postby snielsen » Wed Nov 18, 2009 3:13 pm

Hi,

I have a problem with the projection and toPixels-method. It seems like my projection isn't
working. This is my onCreate-method in my MapActivity:

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);

_mapView = (MapView)findViewById(R.id.mapView);
_mapView.setBuiltInZoomControls(true);

_mapController = _mapView.getController();
double lat = 35.908138*1E6;
double lng = 14.500975*1E6;

_gp = new GeoPoint((int)lat,(int)lng);
Point p = _mapView.getProjection().toPixels(_gp, null);

// Add overlay
_moodOverlay = new MoodOverlay();
List<Overlay> overlays = _mapView.getOverlays();
overlays.add(_moodOverlay);

_mapController.animateTo(_gp);
_mapController.setZoom(17);

System.out.println("GeoPoint: " + _gp.getLatitudeE6() + "x" +
_gp.getLongitudeE6());
System.out.println("Center of screen: " + p.x + "x" + p.y);
}

The problem is that the toPixels is totally wrong. It print's the
following:

GeoPoint: 35908138x14500975
Center of screen: -344668x21990

What is wrong here?

Thanks in advance.
snielsen
Freshman
Freshman
 
Posts: 3
Joined: Fri Oct 23, 2009 1:00 pm

Top

Postby qlimax » Wed Nov 18, 2009 4:41 pm

try to do

Point p = _mapView.getProjection().toPixels(_gp, null);

after that

_mapController.animateTo(_gp);

when you get the point, the center is not yet set.

bye :)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby snielsen » Wed Nov 18, 2009 4:58 pm

Thanks for the reply.

If I understand you correctly, you want me to get the projection after I have animatedTo my GeoPoint?

This is projection before:
p Point (id=830060802360)
x -344668
y 21990

This is projection after:
p Point (id=830060807320)
x -1378673
y 87959

So it actually gives a much worser result.
snielsen
Freshman
Freshman
 
Posts: 3
Joined: Fri Oct 23, 2009 1:00 pm

Postby qlimax » Thu Nov 19, 2009 12:02 pm

sorry but i think that's because your _gp , at the time you call _mapView.getProjection().toPixels(_gp, null); , is not yet in the center of the screen.


if you make


_mapController.animateTo(_gp);
_mapView.getProjection().toPixels(_gp, null);

there a microsecond between this two instruction, but the animate brings 1-2 seconds to bring the _gp on the center of the screen. do you understand what i mean?

you can try that

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Point p;
  3. GeoPoint _gp;
  4. public void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.map);
  8.  
  9. _mapView = (MapView)findViewById(R.id.mapView);
  10. _mapView.setBuiltInZoomControls(true);
  11.  
  12. _mapController = _mapView.getController();
  13. double lat = 35.908138*1E6;
  14. double lng = 14.500975*1E6;
  15.  
  16. _gp = new GeoPoint((int)lat,(int)lng);
  17.  
  18.  
  19. // Add overlay
  20. _moodOverlay = new MoodOverlay();
  21. List<Overlay> overlays = _mapView.getOverlays();
  22. overlays.add(_moodOverlay);
  23.  
  24. _mapController.animateTo(_gp, new Runnable() {
  25.                        
  26.                         public void run() {
  27.                                 p= _mapView.getProjection().toPixels(_gp, null);
  28.                   Log.v("","GeoPoint: " + _gp.getLatitudeE6() + "x" +_gp.getLongitudeE6());
  29.                   Log.v("","Center of screen: " + p.x + "x" + p.y);
  30.                         }
  31.                 });
  32.  
  33. _mapController.setZoom(17);
  34.  
  35.  
  36. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby snielsen » Thu Nov 19, 2009 2:12 pm

That did the thing for sure, but I remember I read somewhere that it was not recommended to use threads within maps?

Right now I want my overlay to behave in three different ways. I can see in my output that the draw()-method is called about three times (that's actually six times, because of the shadow true/false call). So I've made it create a MapOverlay if my local variable _mapView == null, and update the MapOverlay if _mapView != mapView and reuse the MapOverlay if _mapView == mapView. But mapView seems to be equal always. How can I detect if the map changes - if it is moved, zoomed or even if the screen is rotated?
snielsen
Freshman
Freshman
 
Posts: 3
Joined: Fri Oct 23, 2009 1:00 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests