I think your map center is giving you different values because you are checking it before it has animated/moved to the new position you have set... It's the same reason your getLatitudeSpan() was zero at the time. BUT.. if you are setting the map center anyway, you don't need to get it from the map, just use the coordinates you are setting the map center to, OR wait for the map to be finished moving.. and get all the values from there...

I think it's safe to assume that if your getLatitudeSpan() is zero... then your getMapCenter() is probably not accurate either... but I don't know that for sure.

Here's how I call it... The below has a couple of my own functions in it to set parameters etc... but you would just substitute it for whatever you need to do. After the JSON object is created I just assign it to both my Map Overlay and a list view, so I can view the info on either a Map or in a list.

The line:
sharedFunctions.SendMessage(handler, HandlerReturnMessage, param1, param2, retStr); sends the returned string to a handler in the calling activity which deals with it like this :


The HANDLER IN MY ACTIVITY..

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            try
            {
                setProgressBarIndeterminateVisibility(false);

                String res = (String) msg.obj;

                switch (msg.what)
                {
                    case BistroSharedStatic.MESSAGE_LOCATIONS:
                    {
                        try
                        {
                                        resultsJSON = new JSONArray(res);
                        }
                        catch (Exception ex)
                        {
resultsJSON = new JSONArray(""); // just so it's not null.
                        }
                        break;
                    }
  etc....




SENDING TO THE SERVER:
        try
        {
            InputStream is = null;

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urlString);

            if (needSearchParams)
            {
                String searchParams = getSearchParams();
                searchParams = URLEncoder.encode(searchParams, HTTP.UTF_8);

                nvps.add(new BasicNameValuePair("category", searchParams));
            }

            httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            HttpResponse response = httpclient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            if (entity != null)
            {
                try
                {
                    is = entity.getContent();

                    String line;
                    StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
                    while ((line = reader.readLine()) != null)
                    {
                        builder.append(line);
                    }
                    String retStr = builder.toString();
                    // Now get any messages coming back.
                    if (BistroSharedStatic.DEBUGGING_APP)
                        Log.d("GetResults", retStr);

                    if (handler != null)
sharedFunctions.SendMessage(handler, HandlerReturnMessage, param1, param2, retStr);

                }
                finally
                {
                    entity.consumeContent();
                    is = null;
                }
            }

        }
        catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (handler != null)
sharedFunctions.SendMessage(handler, 0, 0, 0, e.getMessage());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (handler != null)
sharedFunctions.SendMessage(handler, 0, 0, 0, e.getMessage());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            if (handler != null)
sharedFunctions.SendMessage(handler, 0, 0, 0, e.getMessage());
        }





On 27/06/2010 3:12 PM, Pedro Teixeira wrote:
Thanks a lot Brad!

I think it's working now. Though I don't know why my map center retrieves a change map, since before I do all this code I setCenter at some specific coordinates, my ''getMapCenter'' is not the same value. I also set up like in your code the calculation in the server side which makes more sense. I wonder how ur sending back the information to Android. I'm trying a JSONObject but all this parsing is overwhelming.

Anyway, thanks a lot, I'll try to fix this so I ca test the code and the algorithm. By now it seems fine other than that ''center'' issue.

Again, thanks a lot :D


On Jun 27, 2010, at 2:37 AM, Brad Gies wrote:

I hope it's generic for all zoom levels.. it should be :).

How it works is that Google Maps at a zoom level of 1 displays the entire world in 256 pixels... When you go to zoom level 2 you get half the world in 256 pixels, zoom level 3 is 1/4 the world in 256 pixels... which is the same as 1 divided by 2 (to the power of zoom level - 1), so the Math.pow(2, zoomLevel - 1) just gets the number that corresponds to what you need to adjust by for the zoom level.

All these functions could be (and will be) combined into about 3 lines for production.. but I just needed to step through it one step at a time to test it out.

The double entireWorld = zoomToPower * 256; just calculates how many pixels it would take to show the entire world (height or width)

and then I just brought it down to 1 degree so I could check the calculations against a map and see what I was doing :).
double OneDegree = entireWorld / 360;

This calculates how much of a degree your screen will show for both the height and width of your screen. I subtracted 40 pixels from the height because that's roughly what my activity uses... and I didn't care if I was exact.. I only want very close. I use it to call a web service and I pass the Map Center latitude and longitude to the web service along with the screen size (in degrees),
       double heightSpan = (display.getHeight() - 40) / OneDegree;
       double widthSpan = display.getWidth() / OneDegree;



and this is how I used it. Because my function returns the fraction of a degree that will show on the screen, I adjust the getLatitudeSpan() function to pass the same value by dividing it by 1E6. Again that should be combined for production.

LatLonSpans latLonSpans = getSpans();
        if (mapView.getLatitudeSpan() == 0)
        {
nvps.add(new BasicNameValuePair("latitudespan", String.valueOf(latLonSpans.LatSpan)));
        }
        else
        {
            double latSpan = mapView.getLatitudeSpan() / 1E6;
nvps.add(new BasicNameValuePair("latitudespan", String.valueOf(latSpan)));
        }
 also do the Longitude....
    and latLonSpans = null;


My function returns the percentage of a degree that your entire screen will show, so I pass the map center lat and lon to the server with the latLonSpan values and then on the server end, I use almost the same calculation you do:

int topLatitude = mapCenter (latitude) + (latSpanPassed /2);
int bottomLatitude = mapCenter (latitude) - (latSpanPassed/2);
int leftLongitude = mapCenter (longitude) - (lonSpanPassed /2);
int rightLongitude = mapCenter (longitude) + (lonSpanPassed /2);




On 26/06/2010 8:47 AM, Pedro Teixeira wrote:
Hi Brad,

Thanks for your solution. I'm trying to understand how it works.
Is this generic for all zoom levels?
I don't know how to implement the if condition snippet of your code. Something is missing me or I'm not understanding it right.
Can you explain this value:
latSpan/1E6, is this the simple getLatitudeSpan?

I'm sorry, I'm a little bit overwhelmed with this problem for some days and I've been really confused.

On Jun 26, 2010, at 3:53 AM, Brad Gies wrote:

Pedro,

I've been working on roughly the same thing today... see the thread MapView.getLatitudeSpan..

All you're running into is that the map isn't ready when you are asking for the getLatitudeSpan, so you need to wait until it is ready...OR...
the formula I've come up with to calculate it is this :

Note that I don't expect this formula is perfect.. and I can certainly make it more compact and faster by reworking it, but for working on it I wanted to do it step by step :).

  private class LatLonSpans
  {
      @SuppressWarnings("unused")
      public double LatSpan = 0.0;
      @SuppressWarnings("unused")
      public double LonSpan = 0.0;
  }

  private LatLonSpans getSpans()
  {
      LatLonSpans latLonSpans = new LatLonSpans();
      Display display = getWindowManager().getDefaultDisplay();
      int width = display.getWidth();
      int height = display.getHeight();


      int zoomLevel = mapView.getZoomLevel();
// zoomToPower times 256 should be equal to the pixels required to view the entire world
      // at the resolution I've set.
      double zoomToPower = Math.pow(2, zoomLevel - 1);
// it takes 256 pixels to view the entire world at resolution 1, so this should be the pixels required to view the entire world
      // at the resolution I've set.
      double entireWorld = zoomToPower * 256;
// Now I need to scale the zoomToPower to my screen size to figure
      // out what I'm really going to see.
      //
      // this is how many pixels to see 1 degree.
      double OneDegree = entireWorld / 360;
// and for height we have x pixels minus 40???? to account for top bars..... so we will see this much of a degree.
      double heightSpan = (height - 40) / OneDegree;
      double widthSpan = width / OneDegree;

      latLonSpans.LatSpan = heightSpan;
      latLonSpans.LonSpan = widthSpan;

      return latLonSpans;
  }

and I'm using it like this :

       if (mapView.getLatitudeSpan() == 0)
       {
nvps.add(new BasicNameValuePair("latitudespan", String.valueOf(latLonSpans.LatSpan)));
       }
       else
       {
nvps.add(new BasicNameValuePair("latitudespan", String.valueOf(latSpan / 1E6)));
       }

Hope it helps... and if anyone can poke holes in my formula .. PLEASE DO... I'd love to know it doesn't work BEFORE I start relying on it. :).




On 25/06/2010 5:55 PM, Pedro Teixeira wrote:
New approach that makes more sense:

int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();
                GeoPoint mapCenter = mapView.getMapCenter();
int topLatitude = mapCenter.getLatitudeE6() + (latSpan/2);
int bottomLatitude = mapCenter.getLatitudeE6() - (latSpan/2);
int leftLongitude = mapCenter.getLongitudeE6() - (latSpan/2);
int rightLongitude = mapCenter.getLongitudeE6() + (latSpan/2);

But this values:
int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();

are giving me 36000000 and 0 :/

On Jun 25, 2010, at 9:15 PM, Frank Weiss wrote:

Exactly. You need to pass the neLatitude, neLongitude, swLatitude,
swLongitude as query parameters to your back end web service, which
then returns only POIs whose latitude and longitude fall within those
bounds, like this Javascript Google Map does:

http://myhome.bankofamerica.com/?findmlo=94114#/findmlo/94105

Notice that it even handles panning and zooming.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Pedro Teixeira

www.pedroteixeira.org <http://www.pedroteixeira.org>

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
Sincerely,

Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------

Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Pedro Teixeira

www.pedroteixeira.org


--
Sincerely,

Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------

Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Pedro Teixeira

www.pedroteixeira.org


--
Sincerely,

Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------

Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to