1

I have a MapImageViewer Activity class that has methods for checking the phones gps location. This activity then calls a custom view of mine called MapCanvas - where the constructor takes the latitude and longitude and then draws a circle to the corresponding pixels on an image of a map.

This works ok, but I am wondering how can I update and call the onDraw method every time the gps coordinates change? I know it needs to go in the onLocationChanged method..but i'm not sure how i can pass the new latitude and longitude values from there to my Custom View class.

Kara
  • 6,115
  • 16
  • 50
  • 57
Mr X
  • 171
  • 1
  • 3
  • 7

2 Answers2

0

Call the view's invalidate() method every time you get new coordinates (or postInvalidate() if the locationListener is in a different thread from the UI thread)

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • I don't see how calling the views invalidate (or postInvalidate) method will do anything. – Mr X Jul 11 '11 at 14:55
  • It tells the operating system to redraw the view thus calling the view's onDraw() method as well as any other behind-the-scenes preparation the operating system must do. invalidate is how you force draw a view. – DeeV Jul 11 '11 at 14:58
  • I tried putting this in my onLocationChanged method and it didn't work.. mapCanvas = new MapCanvas(MapImageViewer.this, myMapImagePath, latitude, longitude); setContentView(mapCanvas); mapCanvas.invalidate(); – Mr X Jul 11 '11 at 15:04
  • scratch that..i mean..I tried putting this in my onLocationChanged method and it didn't work.. mapCanvas.setLat(latitude); //new lat value mapCanvas.setLon(longitude); //new longitude value mapCanvas.invalidate(); //redraw view – Mr X Jul 11 '11 at 15:14
  • invalidate is how you get onDraw() called on a view. Assuming MapCanvas extends MapView, it seems like that would work. I've only added overlays to MapViews though. I've never actually drawn anything directly. I would send a message to LogCat in the onDraw() method to ensure it's getting called. If it is, then ensure the map is being drawn properly. – DeeV Jul 11 '11 at 15:23
  • MapCanvas extends View because it is my custom view. Inside that class is a constructor (context, imagePath, lat, lon) and an onDraw() method. I'm using a custom map image and not google maps. – Mr X Jul 11 '11 at 15:26
  • it works now. It must have been some dodgy gps readings the first time I tried it – Mr X Jul 11 '11 at 17:36
0

What about sending lat/lon from the onLocationChanged() method by using .sendBroadcast() and handle them inside your View class with a BroadcastReceiver?

Alternatively you could use a callback method from the LocationListener like shown in this post.

Community
  • 1
  • 1
iDroid
  • 10,403
  • 1
  • 19
  • 27
  • isn't the sendBroadcast() and receiveBroadcast() used when creating intents? In my case I am setting my content view to my Custom View..and in the Custom View class I need to pass in the new long/lat values..and call onDraw on every change.. – Mr X Jul 11 '11 at 15:07