TourGuide - Virtual Sightseeing


SDK Version: 
M5

This application is a simple application with a Spinner select a location and view that location on a MapView. Click here to download the complete source.

Here is what this TourGuide application looks like:

The main class "TourGuide" is a MapActivity. Only a MapActivity is permitted to create a MapView, but for some reason, you are not permitted to use a MapView XML element. However, I found a workaround in this post on android-developers. If you use a <MapView> element you will get an error, but if you use a <view> and separately specify the class as "com.google.android.maps.MapView" it will work.

Here is the main.xml layout file that we'll use for this application:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.  
  6.         <view class="com.google.android.maps.MapView"
  7.                 android:id="@+id/map"
  8.                 android:layout_width="fill_parent"
  9.                 android:layout_height="fill_parent"/>
  10.        
  11.         <Spinner android:id="@+id/spinner1"
  12.                 android:layout_alignParentTop="true"
  13.                 android:layout_width="fill_parent"
  14.                 android:layout_height="wrap_content"
  15.                 android:drawSelectorOnTop="true"
  16.                 android:paddingTop="10dip"
  17.                 android:paddingBottom="10dip" />
  18.  
  19. </RelativeLayout>

Our layout is a RelativeLayout with a MapView that fills the whole screen, and a spinner floating on top of it at the top of the screen. Now we must create our MapActivity in TourGuide.java:

  1. public class TourGuide extends MapActivity {
  2.  
  3.         private String[][] locations = { { "Area 51", "-115.800155,37.248040&quot; },
  4.                         { "Bill Gates' house", "-122.242135,47.627787&quot; },
  5.                         { "Shepshed Dynamo Football Grounds", "-1.286913,52.774472"; },
  6.                         { "Michael Jackson's Neverland Ranch", "-120.088012,34.745527&quot; },
  7.                         { "Leaning Tower of Pisa", "10.396473,43.723002"; },
  8.                         { "Airplane Graveyard", "-110.834026,32.150899&quot; },
  9.                         { "Grand Canyon", "-112.298641,36.142788&quot; },
  10.                         { "Lake Kariba", "27.990417,-17.235252&quot; },
  11.                         { "White House", "-77.036519,38.897605&quot; },
  12.                         { "World Trade Center site", "-74.012253,40.711641&quot; },
  13.                         { "Las Vegas Strip", "-115.162296,36.133347&quot; } };
  14.  
  15.         private Spinner spinner;
  16.         private MapView map;
  17.         private MapController mc;
  18.  
  19.         @Override
  20.         public void onCreate(Bundle icicle) {
  21.                 super.onCreate(icicle);
  22.                 setContentView(R.layout.main);
  23.  
  24.                 spinner = (Spinner) this.findViewById(R.id.spinner1);
  25.                 map = (MapView) findViewById(R.id.map);
  26.                 mc = map.getController();
  27.  
  28.                 ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
  29.                                 this, android.R.layout.simple_spinner_dropdown_item);
  30.  
  31.                 for (int i = 0; i < locations.length; i++)
  32.                         adapter.addObject(locations[i][0]);
  33.  
  34.                 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  35.                 spinner.setAdapter(adapter);
  36.                 spinner.setOnItemSelectedListener(selectListener);
  37.                 gotoSelected();
  38.         }
  39.  
  40.         private OnItemSelectedListener selectListener = new OnItemSelectedListener() {
  41.                 public void onItemSelected(AdapterView parent, View v, int position, long id) {
  42.                         gotoSelected();
  43.                 }
  44.                 public void onNothingSelected(AdapterView arg0) {}
  45.         };

So first I've just defined an array of Strings called location starting on line 18. It's a 2d array with each entry having the label for the location and the latitude/longitude. I grabbed these from http://www.satellite-sightseer.com/ 's most popular destinations. The exact coordinates can be found in the kml file for each one.

In the onCreate() function we first initialize our MapView and Spinner. After that we create an ArrayAdapter to use with the Spinner and add each label from the locations array to this ArrayAdapter. Once it's filled in we set it as the ArrayAdapter for the Spinner and then call gotoSelected() to move the map to the currently selected item (Area 51 when you first start this app in this example). Finally we setup the Spinner's OnItemSelectdListener so that it will call gotoSelected() when a new item is selected.

Here is the gotoSelected() function that will move our map to the correct location:

  1. public void gotoSelected() {
  2.         int pos = spinner.getSelectedItemPosition();
  3.  
  4.         String[] loc = locations[pos][1].split(",");
  5.         double lat = Double.parseDouble(loc[1]);
  6.         double lon = Double.parseDouble(loc[0]);
  7.  
  8.         Point p = new Point((int) (lat * 1E6), (int) (lon * 1E6));
  9.         mc.animateTo(p);
  10.         mc.zoomTo(18);
  11.  
  12.         if (!map.isSatellite())
  13.                 map.toggleSatellite();
  14.  
  15.         map.invalidate();
  16. }

To go to a location we must create a Point using the latitude and longitude in microdegrees (10^-6). So we get the coordinates from the locations array string for the selected object and split it to get the seperate longitude and latitude as a double. Once we have these we must multiply each one by 1,000,000 to get it's value in microdegrees. Now that we have the Point object we can use a MapController to go to our point using animateTo(Point) and a zoom level of 18 using zoomTo(Int). Finally we make sure that we are showing satellite view instead of map view, and we call map.invalidate() to force it to redraw.

With this you would be able to go view different objects and pan around using the touchscreen. But pressing "I" or "O" doesn't zoom like it normally does, so we must handle those keys to allow the user to zoom in and out. We can simply override the onKeyDown function, and here is that function:

  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.         switch (keyCode) {
  4.         case KeyEvent.KEYCODE_I:
  5.                 mc.zoomTo(map.getZoomLevel() + 1);
  6.                 break;
  7.         case KeyEvent.KEYCODE_O:
  8.                 mc.zoomTo(map.getZoomLevel() - 1);
  9.                 break;
  10.         }
  11.         return super.onKeyDown(keyCode, event);
  12. }

Very simple switch statement here, if the user pressed "I" we zoom in, and they press "O" we zoom out.

I hope you enjoyed this simple application to demonstrate using a MapView. Obviously it's not ideal to just have a bunch of hard-coded locations, but it could be extended to pull and parse some XML from a source on the internet or could be populated by performing a Search on keywords. If you have any questions/comments/suggestions check out the tutorials forum.

Good luck in the challenge!!

Comments

While browsing through all those funny quotes on this site a thought struck my mind. How does one keep coming up with such mind boggling yet brilliant quotes. Personally I think the key is to relax and have fun with what you are doing. The good ideas tend to come when you least expect it.website optimisation

Space has become a battlefield ,website optimisation when the flash off when the hurricane ’s shadow and Jian spread out when nike shox back from the surprise of God , the clouds have g Jing thousand feet in an instant .

Admiring plenty of time as well as you add into your blog and details you are offering! I most certainly will bookmark your website and also have my local freinds check here often. Thumbs up .
megaupload search

These advocates said that how teachers taught students and disciplined them mattered. How security guards followed children mattered. How educators placed students in classes and programs mattered. How principals responded to peer harassment mattered. How superintendents distributed resources to school buildings mattered. Like the white parents filing disability cases, parents and advocates for students of color demanded what I call everyday justice. They asked educators to provide specific students with particular additional opportunities to learn and thrive in their daily lives.

austin moving companies

It's valuable information and helpful Thank you for this add-on
العاب تلبيس

nice article...
by
sarang semut

very nice post! this is very useful! thanks :)

behel lepas pasang

very nice post! it's very useful

heavenrooms.com | 5770 benchmark

My first visit to your site is been a big help.

My blog : Web Design Miami | Miami Web Design

HAVERTOWNFORJESUS.ORG - is a web site bonus for those bitten classic film, students, lovers of pigs and anyone interested in the great films of the last century. Media Engine Detailed synopsis plot, comments and reference review of the film are some of the functionalities available for the site.Media Search Engines The site also contains the analysis of movies, original content, information on the best films and most memorable movie scenes, the "best of" articles, and citations of the most popular film in all genres of films

The main class "TourGuide" is a MapActivity. Only a MapActivity is permitted to create a MapView, but for some reason, you are not permitted to use a MapView XML element. However, I found a workaround in this post on android-developers.
Concesionarios Audi

this site is verry cool!
Masariq Blog

I start reading your articles and other content each and every morning. The website content hits on virtually all the important details and furthermore it is helpful. Terrific. download movies

Keuken renovatie De mogelijkheden voor keuken renovatie bij Visker Keukens zijn haast oneindig. Uw keuken zal als nieuw zijn en exact zoals u wilt. Een keuken renovatie van Visker Keukens is nog echt maatwerk: geen twee keukens zijn hetzelfde. Onze vakmensen maken van uw keuken weer een prachtig plaatje, u kunt weer echt genieten van uw keuken.

Especially TourGuide are one of the most essential requirement which will definitely provides assistance, information and cultural, historical contemporary heritage interpretation.Well elaboration in each and every steps for better understanding. bed bugs

On of the very important part in this tutorial was at this line mc.zoomTo(map.getZoomLevel() + 1), i was getting too much errors at this, iPhone is a life savior, without i couldn't come to this tutorial.

This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.
myegy
ماى ايجى
ماي ايجي
my egy

This is one of the best post that I had ever read on web of this topic. You have done an excellent job here by posting such an informative post. Keep sharing buddy... Optimasi SEO

This tour guide is a method for virtual sightseeing.I am glad to know all these new trends which are attracting users towards androids and I think its better to efficiently use all these features for your benefits.
plenty of fish

Really Really Thanks For This Wonderful article. Its glad that nowadays also, there is some original content on the internet. Otherwise, everywhere you go you will find duplicate content. Thanks Again. Like to Read You article and blog.

movie download | mediafire links | all buzzy news

I now have a clear idea on what this matter is all about. Thanks for Share With Irfan and so you can read my last post daftar blog dofollow 2011 terbaru on my blog.

Interesting article, please add some
free tv shows

Very interesting information
free tv shows

villas in nevisProperty Nevis Island for sale and rent. Villas and apartments for sale and rental. Paradise island of Nevis is one of the best kept secrets of the Caribbean.

That’s pretty good article, I really like the tips you have given.Will be referring a lot of friends about this.Thanks a lot for sharing.Keep blogging.
Edinburgh BMX Shop

I use these sites, they all work for anything. Many of the proxies posted on here have alot of popups or anoying adware and the displayed website is all messed up. This proxy shows the webpage as is, the only ads are on the main page
Dog Life Jacket

I've found the post to be very useful lesson on this mapactivity. I appreciate the excellent work in this regard. Your tourguide, a virtual sightseeing is really very helpful and informative article. thanks. business valuations

nice tips ....

hopefully in the near future I can buy hp android ...
aamiin:)

regards
install ubuntu

This weblog is solely wonderful, I believed I know a lot, but I’m so improper, like the old saying the more you realize, the extra you learn the way little you know. Thanks for the info.
Aqiah di Jakarta- catering aqiqah-PercetakanPercetakan di tangerang- Aqiqah - Aqiqah Jakarta -Madu anak

spent some time browsing forums about hacking android,.. and now I know, that it won't run on mips Data Safes

Thanks for sharing information about "TourGuide - Virtual Sightseeing" application.

GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları

thanks a lot, I have not quite understand about your article but from the little that I read I can understand what she meant,
Mothers Day Flowers

This article is really impressive and interesting.Thanks you very much for shearing this information.
by sarang semut

Steroid-Sale.Com is a legal online Cheap Anabolic Steroids Supplier. All our products comes directly from manufacturers and best legit pharmacies. In our pharmacy you can buy steroids and not only. We colaborate with the best steroids world brands: Geneza Pharmaceuticals, Sciroxx, Iran Hormone, Gen-Shi Laboratories, Balkan Pharmaceuticals, Axiolabs and other. Steroid-Sale.Com offers exclusive high quality legal steroids.

Wow, finally i found it..
thanks so much :)

Interesting post and I really like your take on the issue. I now have a clear idea on what this matter is all about. Thank you so much. get facebook fans

i've tried this application, and it's strongly recommended application

zippy designs

I use these sites, they all work for anything. Many of the proxies posted on here have alot of popups or anoying adware and the displayed website is all messed up. This proxy shows the webpage as is, the only Bankruptcy San Diego

Very informative and useful article indeed. I really like the way writer has presented his views. I hope to see more great articles in future as well.mobility scooters

nice picture...excellent
hiking
belanja
electronic

I think the concept with Java was good, but it hasn't been followed.
The problem is that everyone creating a new platform wants to make their own API's for accessing various functionality. And this is silly.
All platforms have a screen for example. So why can't I draw a line on all those platforms using the same command? Because the creators chose not to do it like that, and thus renders Java no better than C/C++.
Property Investment

I think the concept with Java was good, but it hasn't been followed.
The problem is that everyone creating a new platform wants to make their own API's for accessing various functionality. And this is silly.
All platforms have a screen for example. So why can't I draw a line on all those platforms using the same command? Because the creators chose not to do it like that, and thus renders Java no better than C/C++.
Property Investment

While reading your blog it seems that you research on this topic very much. I must tell you that your blog is very informative and it helps other also.
by:
belajar komputer
trik komputer

Considerably, this post is really the sweetest on this notable topic. I harmonise with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay informed of any updates. Admirable work and much success in your business dealings!  Please excuse my poor English as it is not my first tongue.

Javahostindo Web Hosting Indonesia
JJavahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia

Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia

Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia
Javahostindo Web Hosting Indonesia

avahostindo Web Hosting Indonesia