Android Open Source - advanced-tourist-map Poi Browser Activity






From Project

Back to project page advanced-tourist-map.

License

The source code is released under:

GNU General Public License

If you think the Android project advanced-tourist-map listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011 mapsforge.org//  w ww .j  a  v  a  2  s.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.muxe.advancedtouristmap.poi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.mapsforge.core.GeoCoordinate;
import org.mapsforge.poi.PoiCategory;
import org.mapsforge.poi.PointOfInterest;
import org.muxe.advancedtouristmap.AdvancedTouristMap;
import org.muxe.advancedtouristmap.BaseActivity;
import org.muxe.advancedtouristmap.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PoiBrowserActivity extends BaseActivity {

  private static final String IMAGEKEY = "image";
  private static final String NAMEKEY = "name";
  private static final String INFOKEY = "info";
  private static final String TAG = PoiBrowserActivity.class.getSimpleName();
  private static final String SAVESTATECATEGORY = "saved_current_category";
  private static final int MAX_POIS = 15;

  private double latitude;
  private double longitude;
  String currentCategory;
  ArrayList<PoiOrCategory> poisAndCategories;
  private ListView poiListView;
  private SimpleAdapter poiListAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.advancedMapViewer.setViewWithHelp(this, R.layout.activity_poi_browser);
    this.poiListView = (ListView) findViewById(R.id.poi_browser_poi_list);

    this.currentCategory = "Root";

    Intent startingIntent = getIntent();
    this.latitude = startingIntent.getDoubleExtra("lat", 0.0);
    this.longitude = startingIntent.getDoubleExtra("lon", 0.0);
  }

  @Override
  protected void onResume() {
    super.onResume();
    browseToCurrentCategory();
  }

  void browseToCurrentCategory() {
    // list to hold categories and pois currently displayed
    this.poisAndCategories = new ArrayList<PoiOrCategory>();
    // get the current category by it's name
    PoiCategory currCategory = this.advancedMapViewer.getPerstManager().getCategory(
        this.currentCategory);
    if (currCategory == null) {
      return;
    }
    // this list will hold all the information to fill our adapter
    List<HashMap<String, Object>> fillPois = new ArrayList<HashMap<String, Object>>();
    ArrayList<PoiCategory> childCategories = new ArrayList<PoiCategory>(
        this.advancedMapViewer.getPerstManager().directChildren(this.currentCategory));

    // check if category has parent and set it as first element, to browse up tree
    if (currCategory.getParent() != null) {
      childCategories.add(0, currCategory.getParent());
    }
    for (PoiCategory category : childCategories) {
      HashMap<String, Object> map = new HashMap<String, Object>();
      if (currCategory.getParent() == category) {
        map.put(IMAGEKEY, R.drawable.ic_menu_back);
        map.put(NAMEKEY, "..");
      } else {
        map.put(IMAGEKEY, R.drawable.ic_menu_archive);
        map.put(NAMEKEY, category.getTitle());
      }
      fillPois.add(map);
      this.poisAndCategories.add(new PoiOrCategory(category));
    }

    Iterator<PointOfInterest> iterator = this.advancedMapViewer.getPerstManager()
        .neighborIterator(new GeoCoordinate(this.latitude, this.longitude),
            this.currentCategory);
    for (int i = 0; i < MAX_POIS && iterator.hasNext(); i++) {
      PointOfInterest poi = iterator.next();
      HashMap<String, Object> map = new HashMap<String, Object>();
      // calculate the distance for display purposes
      int distance = (int) GeoCoordinate.sphericalDistance(this.longitude, this.latitude,
          poi.getLongitude(), poi.getLatitude());
      String description;
      if (poi.getName() != null) {
        // display name and category
        description = poi.getName() + " (" + poi.getCategory().getTitle() + ")";
      } else {
        // poi has no name, so just dosplay the category (e.g. Telephones)
        description = poi.getCategory().getTitle();
      }
      map.put(IMAGEKEY, R.drawable.ic_menu_myplaces);
      map.put(NAMEKEY, description);
      map.put(INFOKEY, distance + " m");
      fillPois.add(map);
      this.poisAndCategories.add(new PoiOrCategory(poi));
    }

    String[] from = new String[] { IMAGEKEY, NAMEKEY, INFOKEY };
    int[] to = new int[] { R.id.poi_row_image, R.id.poi_row_name, R.id.poi_row_distance };

    this.poiListAdapter = new SimpleAdapter(this, fillPois, R.layout.poi_row, from, to);
    this.poiListView.setAdapter(this.poiListAdapter);
    this.poiListView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        PoiOrCategory poiOrCat = PoiBrowserActivity.this.poisAndCategories
            .get(position);
        if (poiOrCat.isPoiCategory()) {
          PoiBrowserActivity.this.currentCategory = poiOrCat.getPoiCategory()
              .getTitle();
          PoiBrowserActivity.this.browseToCurrentCategory();
        } else {
          // startActivity(new Intent(PoiBrowserActivity.this, PositionInfo.class)
          // .putExtra("LATITUDE", poiOrCat.getPoi().getLatitude()).putExtra(
          // "LONGITUDE", poiOrCat.getPoi().getLongitude()));
          PoiBrowserActivity.this.advancedMapViewer.getCurrentPois().clear();
          PoiBrowserActivity.this.advancedMapViewer.getCurrentPois().add(
              poiOrCat.getPoi());
          startActivity(new Intent(PoiBrowserActivity.this, AdvancedTouristMap.class)
              .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("CENTER_POI", true));
        }
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.poi_browser_menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_show_pois_map:
        this.advancedMapViewer.getCurrentPois().clear();
        for (PoiOrCategory poiOrCat : this.poisAndCategories) {
          if (poiOrCat.isPoi()) {
            this.advancedMapViewer.getCurrentPois().add(poiOrCat.getPoi());
          }
        }
        startActivity(new Intent(PoiBrowserActivity.this, AdvancedTouristMap.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        return true;
      default:
        return false;
    }
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String savedCategory = savedInstanceState.getString(SAVESTATECATEGORY);
    if (savedCategory != null) {
      this.currentCategory = savedCategory;
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    outState.putString(SAVESTATECATEGORY, this.currentCategory);
    super.onSaveInstanceState(outState);
  }
}




Java Source Code List

org.mapsforge.geocoding.Unchecked.java
org.mapsforge.geocoding.widget.CityCompletionAdapter.java
org.mapsforge.geocoding.widget.PlaceCompletionAdapter.java
org.mapsforge.geocoding.widget.RoadCompletionAdapter.java
org.mapsforge.geocoding.widget.RoadListAdapter.java
org.mapsforge.geocoding.widget.SqliteCompletionAdapter.java
org.mapsforge.geocoding.widget.State.java
org.muxe.advancedtouristmap.AdvancedTouristMapApplication.java
org.muxe.advancedtouristmap.AdvancedTouristMap.java
org.muxe.advancedtouristmap.BaseActivity.java
org.muxe.advancedtouristmap.CacheSizePreference.java
org.muxe.advancedtouristmap.EditPreferences.java
org.muxe.advancedtouristmap.FilePickerIconAdapter.java
org.muxe.advancedtouristmap.FilePicker.java
org.muxe.advancedtouristmap.InfoView.java
org.muxe.advancedtouristmap.LocationPicker.java
org.muxe.advancedtouristmap.MoveSpeedPreference.java
org.muxe.advancedtouristmap.PositionInfo.java
org.muxe.advancedtouristmap.Search.java
org.muxe.advancedtouristmap.SeekBarPreference.java
org.muxe.advancedtouristmap.Utility.java
org.muxe.advancedtouristmap.overlay.GenericOverlayItem.java
org.muxe.advancedtouristmap.overlay.GenericOverlay.java
org.muxe.advancedtouristmap.overlay.PoiOverlayItem.java
org.muxe.advancedtouristmap.overlay.PositionOverlayItem.java
org.muxe.advancedtouristmap.overlay.WikiOverlayItem.java
org.muxe.advancedtouristmap.poi.PoiBrowserActivity.java
org.muxe.advancedtouristmap.poi.PoiOrCategory.java
org.muxe.advancedtouristmap.routing.AngleCalc.java
org.muxe.advancedtouristmap.routing.DecisionOverlay.java
org.muxe.advancedtouristmap.routing.DecisionPoint.java
org.muxe.advancedtouristmap.routing.RouteCalculator.java
org.muxe.advancedtouristmap.routing.RouteList.java
org.muxe.advancedtouristmap.routing.Route.java
org.muxe.advancedtouristmap.sourcefiles.AddressFile.java
org.muxe.advancedtouristmap.sourcefiles.FileManagerActivity.java
org.muxe.advancedtouristmap.sourcefiles.FileManager.java
org.muxe.advancedtouristmap.sourcefiles.MapBundle.java
org.muxe.advancedtouristmap.sourcefiles.MapFile.java
org.muxe.advancedtouristmap.sourcefiles.PoiFile.java
org.muxe.advancedtouristmap.sourcefiles.RoutingFile.java
org.muxe.advancedtouristmap.sourcefiles.SourceFile.java
org.muxe.advancedtouristmap.wikipedia.AbstractWikiArticle.java
org.muxe.advancedtouristmap.wikipedia.ArticleRetrieverFactory.java
org.muxe.advancedtouristmap.wikipedia.ArticleRetriever.java
org.muxe.advancedtouristmap.wikipedia.GeonamesRetriever.java
org.muxe.advancedtouristmap.wikipedia.OnlineWikiArticle.java
org.muxe.advancedtouristmap.wikipedia.WikiArticleInterface.java
org.muxe.advancedtouristmap.wikipedia.WikilocationRetriever.java