Android Open Source - bike-friend Station Map Activity






From Project

Back to project page bike-friend.

License

The source code is released under:

GNU General Public License

If you think the Android project bike-friend 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

package com.lemoulinstudio.bikefriend;
/*ww  w  .ja  v a2 s.co  m*/
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.lemoulinstudio.bikefriend.cbike.KaohsiungStationProvider;
import com.lemoulinstudio.bikefriend.ubike.ChanghuaStationProvider;
import com.lemoulinstudio.bikefriend.ubike.TaichungStationProvider;
import com.lemoulinstudio.bikefriend.ubike.TaipeiStationProvider;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Vincent Cantin
 */
public class StationMapActivity extends FragmentActivity {

  // Tag for the debug messages of the app.
  public static final String LOG_TAG = "bikeFriend";
  
  // Keys for the settings of the app.
  private static final String CAMERA_TARGET_LAT = "cameraTargetLat";
  private static final String CAMERA_TARGET_LNG = "cameraTargetLng";
  private static final String CAMERA_ZOOM = "cameraZoom";
  private static final String CAMERA_TILT = "cameraTilt";
  private static final String CAMERA_BEARING = "cameraBearing";
  
  private GoogleMap map;
  private StationInfoWindowAdapter siwa;
  
  private StationProvider taipeiProvider = new TaipeiStationProvider();
  private StationProvider changhuaProvider = new ChanghuaStationProvider();
  private StationProvider taichungProvider = new TaichungStationProvider();
  private StationProvider kaohsiungProvider = new KaohsiungStationProvider();
  
  private List<StationProvider> stationProviders = Arrays.<StationProvider>asList(
          taipeiProvider, changhuaProvider, taichungProvider, kaohsiungProvider);

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Create the view.
    setContentView(R.layout.station_map);
    siwa = new StationInfoWindowAdapter(this);
  }

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

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_refresh: {
        if (map != null) {
          for (StationProvider stationProvider : stationProviders) {
            stationProvider.refreshData();
          }
        }
        return true;
      }
      case R.id.menu_place_taipei: {
        // TODO: change to fixed bouding box.
        animateCameraToBoundingBox(taipeiProvider.getLatLngBounds());
        return true;
      }
      case R.id.menu_place_changhua: {
        // TODO: change to fixed bouding box.
        animateCameraToBoundingBox(changhuaProvider.getLatLngBounds());
        return true;
      }
      case R.id.menu_place_taichung: {
        // TODO: change to fixed bouding box.
        animateCameraToBoundingBox(taichungProvider.getLatLngBounds());
        return true;
      }
      case R.id.menu_place_kaohsiung: {
        // TODO: change to fixed bouding box.
        animateCameraToBoundingBox(kaohsiungProvider.getLatLngBounds());
        return true;
      }
      case R.id.menu_place_tainan: {
        // TODO: change to fixed bouding box.
        //animateCameraToBoundingBox(...);
        return true;
      }
      case R.id.menu_settings: {
        return true;
      }
      case R.id.menu_about: {
        return true;
      }
      default: {
        return super.onOptionsItemSelected(item);
      }
    }
  }

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

  private void setupMap() {
    if (map == null) {
      map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.station_map)).getMap();

      if (map != null) {
        // Reads the settings.
        SharedPreferences settings = getPreferences(MODE_PRIVATE);
        float cameraTargetLat = settings.getFloat(CAMERA_TARGET_LAT, 25.030142f);
        float cameraTargetLng = settings.getFloat(CAMERA_TARGET_LNG, 121.53549f);
        float cameraZoom = settings.getFloat(CAMERA_ZOOM, 13.0f);
        float cameraTilt = settings.getFloat(CAMERA_TILT, 0.0f);
        float cameraBearing = settings.getFloat(CAMERA_BEARING, 0.0f);

        // Restores the state of the camera on the map.
        CameraPosition cameraPosition = new CameraPosition(
                new LatLng(cameraTargetLat, cameraTargetLng),
                cameraZoom, cameraTilt, cameraBearing);
        
        map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        
        map.setMyLocationEnabled(true);
        map.setInfoWindowAdapter(siwa);
        
        for (StationProvider stationProvider : stationProviders) {
          stationProvider.setMap(map);
          stationProvider.setStationInfoWindowAdapter(siwa);
        }
        
        map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
          public void onCameraChange(CameraPosition cp) {
            for (StationProvider stationProvider : stationProviders) {
              stationProvider.notifyCameraChanged();
            }
          }
        });
      }
    }
  }
  
  protected void onPause() {
    super.onPause();
    
    // Saves the settings.
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    
    if (map != null) {
      // Saves the state of the camera on the map.
      CameraPosition cameraPosition = map.getCameraPosition();
      editor.putFloat(CAMERA_TARGET_LAT, (float) cameraPosition.target.latitude);
      editor.putFloat(CAMERA_TARGET_LNG, (float) cameraPosition.target.longitude);
      editor.putFloat(CAMERA_ZOOM, cameraPosition.zoom);
      editor.putFloat(CAMERA_TILT, cameraPosition.tilt);
      editor.putFloat(CAMERA_BEARING, cameraPosition.bearing);
    }
    
    editor.commit();
  }

  private void animateCameraToBoundingBox(LatLngBounds bounds) {
    if (bounds != null) {
      map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
    }
  }
  
}




Java Source Code List

com.lemoulinstudio.bikefriend.InternetStationProvider.java
com.lemoulinstudio.bikefriend.ParsingException.java
com.lemoulinstudio.bikefriend.StationInfoWindowAdapter.java
com.lemoulinstudio.bikefriend.StationMapActivity.java
com.lemoulinstudio.bikefriend.StationParser.java
com.lemoulinstudio.bikefriend.StationProvider.java
com.lemoulinstudio.bikefriend.Station.java
com.lemoulinstudio.bikefriend.Utils.java
com.lemoulinstudio.bikefriend.cbike.CBikeStationXmlParserV1.java
com.lemoulinstudio.bikefriend.cbike.CBikeStation.java
com.lemoulinstudio.bikefriend.cbike.KaohsiungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.ChanghuaStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaichungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaipeiStationProvider.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationCSVParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationHtmlParserV2.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationJsonParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStation.java
com.lemoulinstudio.bikefriend.webapp.StationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStationData.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStation.java
com.lemoulinstudio.bikefriend.webapp.cbike.CBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.XmlStation.java
com.lemoulinstudio.bikefriend.webapp.conf.ShutdownHook.java
com.lemoulinstudio.bikefriend.webapp.conf.StartupHook.java
com.lemoulinstudio.bikefriend.webapp.entity.StationLog.java
com.lemoulinstudio.bikefriend.webapp.entity.Station.java
com.lemoulinstudio.bikefriend.webapp.io.InputStreamSequence.java
com.lemoulinstudio.bikefriend.webapp.io.IntArrayInputStream.java
com.lemoulinstudio.bikefriend.webapp.quartz.LoadStationDataJob.java
com.lemoulinstudio.bikefriend.webapp.rest.StationResource.java
com.lemoulinstudio.bikefriend.webapp.ubike.MarkerList.java
com.lemoulinstudio.bikefriend.webapp.ubike.Marker.java
com.lemoulinstudio.bikefriend.webapp.ubike.YouBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.vo.StationLogVo.java
com.lemoulinstudio.bikefriend.webapp.vo.StationVo.java