Back to project page interamap.
The source code is released under:
MIT License
If you think the Android project interamap listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.richso.interamap; // w w w .jav a 2 s . c o m import android.graphics.Point; import android.location.Location; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.location.LocationClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.maps.*; import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.richso.interamap.adapter.MarkerInfoAdapter; import com.richso.interamap.utils.L; /** * Created with IntelliJ IDEA. * User: nikolai * Date: 9/19/13 * Time: 4:02 PM * To change this template use File | Settings | File Templates. */ public class MapScreen extends BaseActivity implements OnMyLocationButtonClickListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnInfoWindowClickListener { { L.setTag(this.getClass().getName()); } private GoogleMap map; private LocationClient locationClient; private Marker myLocationMarker; private static LocationRequest REQUEST = LocationRequest.create() .setInterval(3000) .setFastestInterval(16) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); checkGooglePlayServicesAvailability(); initializeMap(); setContentView(R.layout.activity_map); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); setUpLocationClientIfNeeded(); locationClient.connect(); } @Override protected void onPause() { super.onPause(); if( locationClient != null ) { locationClient.disconnect(); } } private void initializeMap() { try { MapsInitializer.initialize(getApplicationContext()); } catch ( GooglePlayServicesNotAvailableException e ) { L.e(e.getMessage()); } } private void checkGooglePlayServicesAvailability() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); switch( status ){ case ConnectionResult.SUCCESS: L.i("Success"); break; case ConnectionResult.SERVICE_MISSING: L.i("SERVICE_MISSING"); showWarningDialog(R.string.title_info, R.string.google_play_service_error_3, R.string.cancel, 0, 0); break; case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: L.i("SERVICE_VERSION_UPDATE_REQUIRED"); showWarningDialog(R.string.title_info, R.string.google_play_service_error_4, 0, R.string.cancel, 0); showWarningDialog(R.string.title_info, R.string.google_play_service_error_2, R.string.cancel, 0, 0); break; case ConnectionResult.SERVICE_DISABLED: L.i("SERVICE_DISABLED"); showWarningDialog(R.string.title_info, R.string.google_play_service_error_5, R.string.cancel, 0, 0); break; default: showWarningDialog(R.string.title_info, R.string.google_play_service_error_1, R.string.cancel, 0, 0); break; } } private void setUpMapIfNeeded() { if( map == null ) { map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap(); if( map != null ) { UiSettings settings = map.getUiSettings(); settings.setCompassEnabled(true); map.setMyLocationEnabled(true); map.setOnMyLocationButtonClickListener(this); map.setInfoWindowAdapter(new MarkerInfoAdapter(MapScreen.this)); map.setOnInfoWindowClickListener(this); map.setOnMarkerClickListener(this); } else { L.e("Map is null"); } } } private void setUpLocationClientIfNeeded() { if( locationClient == null ) { locationClient = new LocationClient(getApplicationContext(), this, this); } } private void setUpMyLocationCamera() { if( locationClient != null && locationClient.isConnected() ) { Location currentLocation = locationClient.getLastLocation(); if( currentLocation != null ) { LatLng currentPosition = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); myLocationMarker = map.addMarker(new MarkerOptions().position(currentPosition)); map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 15)); map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); } else { Toast.makeText(this, R.string.service_google_my_location, Toast.LENGTH_LONG).show(); } } else { Toast.makeText(this, R.string.service_google_my_location, Toast.LENGTH_LONG).show(); } } private void animateMarker(final Marker marker, final Location position, final boolean hideMarker){ final Handler handler = new Handler(); final long start = SystemClock.uptimeMillis(); Projection proj = map.getProjection(); Point startPoint = proj.toScreenLocation(marker.getPosition()); final LatLng startLatLng = proj.fromScreenLocation(startPoint); final long duration = 500; final Interpolator interpolator = new LinearInterpolator(); handler.post( new Runnable() { @Override public void run() { long elapsed = SystemClock.uptimeMillis() - start; float t = interpolator.getInterpolation((float) elapsed / duration); double lng = t * position.getLongitude() + (1 - t) * startLatLng.longitude; double lat = t * position.getLatitude() + (1 - t) * startLatLng.latitude; marker.setPosition(new LatLng(lat, lng)); if ( t < 1.0 ) { handler.postDelayed(this, 16); } else { if ( hideMarker ) { marker.setVisible(false); } else { marker.setVisible(true); } } } }); } @Override public boolean onMyLocationButtonClick() { setUpMyLocationCamera(); return false; } @Override public void onConnected(Bundle bundle) { locationClient.requestLocationUpdates(REQUEST, this); } @Override public void onDisconnected() { //To change body of implemented methods use File | Settings | File Templates. } @Override public void onLocationChanged(Location location) { if( locationClient != null && myLocationMarker != null) { animateMarker(myLocationMarker, locationClient.getLastLocation(), true); } else { L.w("LocationClient is null"); } } @Override public void onConnectionFailed(ConnectionResult connectionResult) { //To change body of implemented methods use File | Settings | File Templates. } @Override public void onInfoWindowClick(Marker marker) { L.i("onInfoWindowClick"); // Intent intent = new Intent(MapAttraction.this, MarkerInfoActivity.class); // intent.putExtra(Constant.MARKER_ID, marker.getId()); // startActivity(intent); } @Override public boolean onMarkerClick(Marker marker) { return false; //To change body of implemented methods use File | Settings | File Templates. } }