com.pansapiens.occyd.MapResults.java Source code

Java tutorial

Introduction

Here is the source code for com.pansapiens.occyd.MapResults.java

Source

//    This file is part of the Occyd Android Client 
//    - an Android application for geolocation tagging
//    Copyright (C) 2008, Andrew Perry (ajperry@pansapiens.com)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.pansapiens.occyd;

import java.util.ArrayList;

import org.json.JSONException;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MapResults extends MapActivity implements LocationListener {
    private MyLocationOverlay mMyLocationOverlay;
    private MapView map;
    LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // receive search results from calling Activity, 
        // as a raw json string
        Bundle incoming_data = getIntent().getExtras();
        String json_txt = null;
        ArrayList<Post> post_results = new ArrayList();
        if (incoming_data != null) {
            json_txt = incoming_data.getString("json");
            try {
                post_results = JSONdecoder.json2postArray(json_txt);
            } catch (JSONException e) {
                Toast.makeText(MapResults.this, "FAIL: Malformed response from server.", Toast.LENGTH_LONG).show();
            }
        }

        setContentView(R.layout.mapview);
        map = (MapView) findViewById(R.id.map);

        // zoom controls are placed in their own 'sub-layout' called
        // "@+id/zoom" in the mapview.xml layout.
        // this ensures they are correctly positioned, don't scroll offscreen
        // and don't prevent touch scrolling of the map when visible 
        LinearLayout zoomView = (LinearLayout) findViewById(R.id.zoom);
        zoomView.addView(map.getZoomControls());

        // (straight out of the MapViewCompassDemo.java API Demos)
        // put a dot on our current location once we have a fix
        mMyLocationOverlay = new MyLocationOverlay(this, map);
        mMyLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                map.getController().animateTo(mMyLocationOverlay.getMyLocation());
            }
        });
        map.getOverlays().add(mMyLocationOverlay);
        map.getController().setZoom(17);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        LocationUpdater locupdater = new LocationUpdater(locationManager, this);
        locupdater.startUpdating();

        // add markers for search results
        // marker from: http://commons.wikimedia.org/wiki/File:Map_symbol_location_02.png
        Drawable marker = getResources().getDrawable(R.drawable.marker);

        marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

        map.getOverlays().add(new MapResultsOverlay(marker, post_results));
        //map.getOverlays().add(new MapResultsOverlay(marker)); // dummy testing constructor ....
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // group, id (for click handling), order, title
        int order = Menu.CATEGORY_CONTAINER + Menu.FLAG_ALWAYS_PERFORM_CLOSE;
        menu.add(Menu.NONE, 0, order, "Search").setAlphabeticShortcut('s');
        ;
        menu.add(Menu.NONE, 1, order, "Post").setAlphabeticShortcut('p');
        ;
        menu.add(Menu.NONE, 2, order, "Map").setAlphabeticShortcut('m');
        menu.add(Menu.NONE, 3, order, "Settings");
        menu.add(Menu.NONE, 4, order, "Help").setAlphabeticShortcut('h');
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent I;
        switch (item.getItemId()) {
        case 0:
            I = new Intent(this, Search.class);
            break;
        case 1:
            I = new Intent(this, NewPost.class);
            break;
        case 2:
            I = new Intent(this, MapResults.class);
            break;
        case 3:
            startActivity(new Intent(this, Prefs.class));
            return true;
        case 4:
            //showAlert("Menu Item Clicked", "Help", "ok", null, false, null);
            return true;
        default:
            return false;
        }

        startActivity(I);
        this.finish();
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

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

    @Override
    protected void onStop() {
        mMyLocationOverlay.disableMyLocation();
        super.onStop();
    }

    public void onLocationChanged(Location location) {
        // this auto-updates the map display to
        // always show your current location
        // TODO: make this a menu option "Follow on/off"
        map.getController().animateTo(mMyLocationOverlay.getMyLocation());
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}