Day 5: CameraView and some Math

As expected, today turned out to be a bunch of experimenting and not a lot of results. I have a general grasp of what I need to do now, but it looks like it may involve a bit of maths.

Also, I’ve noticed that my blog is starting to pop up on Google search results for the sort of things I was searching for myself a few days ago. The code snippets I post here are probably a bit useless when they’re not seen in context with everything else, but then at this point I’m mostly posting them for myself to look back on later on. I plan on writing some tutorials once this is all finished, because there aren’t many out there, but for now, if you happen to come across this and you need some help trying to do similar things to what I’m doing, drop me an email at android@lc8n.co.uk

Today’s Results

Not a lot to show today, but I guess I do have something that could be classed, very very loosely, as an augmented reality application. I’ve moved onto another screen now, the Camera view. The intent is to take those Google search results I had on the maps yesterday, and have them being overlaid in the right place on the map.

I spent a long time playing around with overlay stuff, and trying to understand some maths, but in the end I tested it out, and I now have an overlay that shows you the location of the nearest compass point. That line probably doesn’t make much sense, so here’s some pictures :

(10 minutes later, after first posting the pictures I realised I’d done it wrong! Fixed now)

It also works correctly in Landscape mode now:

Also just to show that it is correct, here’s a comparison in the map view. I am indeed sitting in that location, facing west.

Yes, I do need to fix those buttons.

The Code

Of course, this is just dealing with changes in one dimension. When it comes to labelling buildings, I’m going to need to worry about 2 or 3 dimensions. The labels should move up or down depending on which way you’re pointing the camera, and it also needs to take into account the distance away it is, to get the perspective right. So that close things will move off of the screen if you move it a small distance, and far things stay on the screen across a larger range of movement. For example, on the middle screenshot above, if the ‘West’ label was actually labelling my monitor, I wouldn’t want it on the left screenshot, because it’s off the screen. In the case of compass distance, we’re labelling something infinitely far away though.

The code for working this out is pretty basic at the moment. I read some things about more complex calculations of the direction, but they didn’t seem to work, and right now, I’m able to do what I need to do through simpler means. As long as it works when I’m either holding it upright in portrait and landscape modes, that’s fine. Even just one of those would be ok for now, but it seems to be ok with both. I don’t need to worry about making it work when I’m upside down or laying on the floor. I also saw mention of using the accelerometer, but as of yet I don’t need to. Anyway, here’s an idea of the code for this bit -

if(dir<45)
{
position = (canvas.getWidth()/2) – 50 – (dir *(canvas.getWidth()/90));
canvas.drawText(“NORTH”, position, canvas.getHeight()/2, paint);
}
else if(dir>45&&dir<135)
{
dir = dir-45;
position = canvas.getWidth() – 50 – (dir *(canvas.getWidth()/90));
canvas.drawText(“EAST”, position, canvas.getHeight()/2, paint);
}
else if(dir>135&&dir<225)
{
dir = dir-135;
position = canvas.getWidth() – 50 – (dir *(canvas.getWidth()/90));
canvas.drawText(“SOUTH”, position, canvas.getHeight()/2, paint);
}
else if(dir>225&&dir<315)
{
dir = dir-225;
position = canvas.getWidth() – 50 – (dir *(canvas.getWidth()/90));
canvas.drawText(“WEST”, position, canvas.getHeight()/2, paint);
}
else
{
dir = dir-315;
position = canvas.getWidth() – 50 – (dir *(canvas.getWidth()/90));
canvas.drawText(“NORTH”, position, canvas.getHeight()/2, paint);
}

Looks a bit long winded, but it’s pretty simple. Basically, it’s printing a different string depending on which quarter of the degree range you’re facing. it’s split into 5 because north is from 0-45 degrees and 315 to 0 degrees. In each case, it converts the degrees into a relative position between the two nearest compass points, and then converts that into a position on the screen. Wow, that didn’t make much sense. I’m sure I could do it in a much simpler way, but this was just a basic test for the moment.

I also had a look into the Google search results from yesterday in a bit more detail. There were two issues that came about yesterday. For one, it was showing things that weren’t at all relevant sometimes (pubs when I searched for train stations), and sometimes it wasn’t showing the most relevant things either.

The first issue is fairly easy to filter out. There’s also an ‘accuracy’ value for each search result. The actual train stations had an accuracy of 1, while the pub had an accuracy of 8. So, obviously the lower accuracy value the better. Turns out that the pub actually does have a review that mentions that it’s easy to get to from the train station, which is why it showed up, but fortunately Google has taken care of this issue by calculating this accuracy score.

I also realised that it was only returning 4 search results. To see more you need to specify a “start” value. I’d like to be able to do this without having to send countless search requests, but I’m not sure how easy that would be. Ideally I’d like to be able to sort them by accuracy, and return the top x number of results, or everything with an accuracy below a certain number. Unfortunately Google doesn’t seem to provide the functionality to sort local search results in any other way. You can choose to sort news and blog posts by relevance or date, but it would be nice to see the option to sort local results either by distance or relevance. I guess that could throw up some other issues too though.

Another handy feature is that you can provide it with an area to search. Previously I was just giving it a center point, so it was giving things that were quite a way away sometimes. But again, thanks to the magic of google’s API, you can get the degrees that are covered by your current view, so you can provide these to the search easily. That way, you’re only getting search results that you can actually see right now. In the context of the camera view, you’d probably want to limit it to locations that are pretty close to you, and this could maybe be controllable by the user to say how far away they want to show. I’ll need to work out how to get these sort of numbers into the correct format.

double lat = p.getLatitudeE6()/1E6;
double lng = p.getLongitudeE6()/1E6;
double latspan = mapView.getLatitudeSpan()/1E6;
double lngspan = mapView.getLongitudeSpan()/1E6;
results = gs.runJSONParser(URLEncoder.encode(edit.getText().toString()),lat,lng,latspan,lngspan);

And the code in the search class to run the search -

Reader r = new InputStreamReader(getJSONData(“http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=”+request+”&sll=”+lat+”,”+lng+”&sspn=”+latspan+”,”+lngspan”&rsz=large”));

So, first we’re giving it our search query, then our location. After that, the sspn parameter gives our search area. The rsz parameter of large specifies that we want 8 results. Unfortunately you can only choose between 4 (small) or 8 (large) here. You can then supply &start=9 after that, and so on, to get more results. There is also a field in the JSON response that shows how many results there are, so you know how far to look.

Tomorrow’s Goal

Hopefully make some progress on overlaying the locations on the screen. I doubt I can get it perfect tomorrow, but hopefully something that generally works. I won’t be able to test this too well tomorrow at home, but I’m going into town on Sunday, so I’ll be able to see if it’s working then.

Links of the Day

http://www.devx.com/wireless/Article/42482/1954
This article basically gives you a crash course in starting up an AR application. A lot of it covers the same stuff as my previous posts, but it’s mostly concise and simple. It only covers the backend stuff though, rather than any output.

http://www.devx.com/wireless/Article/43005/
The second part of the above article. As I said, the maths here doesn’t seem to work for me, but so far I haven’t needed it. That may change tomorrow.

http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
A detailed list of the parameters you can provide in each sort of Google search

http://www.mail-archive.com/android-developers@googlegroups.com/msg22032.html
A pretty simple example of showing Overlays on the camera view. Probably irrelevant for most people, unless like me, you jumped into making an AR app without any experience of Android development!

If you have any questions or comments about anything here, feel free to contact me, either via the comments on here, or email at android@lc8n.co.uk

Advertisement

2 Responses to “Day 5: CameraView and some Math”

  1. l_bratch Says:

    Wjoe’s hands! Also those JavaScript mouseover prompt things on the images are very, very annoying.

  2. Joe Says:

    Eh. The only thing I get when I mouseover the images is the standard alt text.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.