When I get the current location lat/long it gives me back the wrong
number..
if the center is for example : 14,324234 23,432557 it delivers
14000000 23000000
That's why my code is all messed up.. I never got the center right.
On Jun 28, 2010, at 11:17 PM, Brad Gies wrote:
I set the zoom level at 15 if I don't already have it. BUT.. I save
the zoom level, latitude, longitude and a bunch of other stuff to
Preferences in the onPause and restore it in onResume, so the only
time I don't have something to work with is the first time a user
uses that activity. I've been doing it that way for so long I don't
remember what the default zoom level is... but I think it's 16. I
remember that I thought it was off by one from what I wanted.
On 28/06/2010 1:40 PM, Pedro Teixeira wrote:
"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..."
Yes, I guess it's the same reason. The map isn't ready fast
enough. My map center is actually the user position, so it will
depend on the device coordinates, so I can't define any position
for it. I'm trying to figure out a way to let the map finishing
moving, before I can set a center, though I don't understand
because it's getting the ''curentlocation'' coordinates just fine..
but the the mapCenter value is different.
By the way.. do you set any zoom on your code? Which is the
predefined zoom if you don't set a zoom at all?
I have my code different from yours.. I guess more ''newbie'' like
but it's kinda working..
I'm just having an issue with my server algorithm because besides
the pictures being in that bound we've been talking about I want
them to be shown only when they have a certain distance between
them ( I called it threshold) .. e.g: if 2 pictures have too close
latitude or longitude just one of them is shown. I had this set on
the server but I'm dealing with the minimal size of the distance
between picture 1 and picture 2, because it depends on the zoom. On
a lower zoom (higher view = less pictures = bigger threshold), on a
bigger zoom (closer view = more pictures = smaller threshold) ...
Basically I think I can summarize my objective with this link:
http://myhome.bankofamerica.com/?findmlo=94114#/findmlo/94105
It's what I'm trying to achieve.
I know that after all this is done I'll still have to deal with a
listener for panning or zooming lol.. good luck for me
On Jun 28, 2010, at 12:19 AM, Brad Gies wrote:
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Pedro Teixeira
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en