Created
April 23, 2016 16:27
-
-
Save aemxn/720ce207dbff2c751aee52e923249403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity | |
implements GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener { | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private SupportMapFragment mapFragment; | |
private MapObservableProvider mapObservableProvider; | |
private CompositeSubscription subscriptions = Subscriptions.from(); | |
private GoogleMap googleMap; | |
private GoogleApiClient mGoogleApiClient; | |
private Location mCurrentLocation; | |
private LatLng mCenterLocation; | |
private Context context = MainActivity.this; | |
private final int[] MAP_TYPES = {GoogleMap.MAP_TYPE_SATELLITE, | |
GoogleMap.MAP_TYPE_NORMAL, | |
GoogleMap.MAP_TYPE_HYBRID, | |
GoogleMap.MAP_TYPE_TERRAIN, | |
GoogleMap.MAP_TYPE_NONE}; | |
private int curMapTypeIndex = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initFragment(); | |
initGoogleApiClient(); | |
observeEvents(); | |
initMarkers(); | |
} | |
private void observeEvents() { | |
mapObservableProvider = new MapObservableProvider(mapFragment); | |
/** Map Ready */ | |
subscriptions.add(mapObservableProvider.getMapReadyObservable() | |
.subscribe(googleMap -> { | |
this.googleMap = googleMap; | |
})); | |
/** Long click */ | |
subscriptions.add(mapObservableProvider.getMapLongClickObservable() | |
.subscribe(latLng -> { | |
L.d("map long click: " + latLng.toString()); | |
})); | |
/** Click; add marker */ | |
subscriptions.add(mapObservableProvider.getMapClickObservable() | |
.subscribe(latLng -> { | |
final MarkerOptions options = new MarkerOptions().position(latLng); | |
options.title(Utils.getAddressFromLatLng(context, latLng)); | |
options.icon(BitmapDescriptorFactory.defaultMarker()); | |
googleMap.addMarker(options); | |
})); | |
/** Marker click */ | |
subscriptions.add(mapObservableProvider.getMarkerClickObservable() | |
.subscribe(Marker::showInfoWindow)); | |
/** Show info window */ | |
subscriptions.add(mapObservableProvider.getInfoWindowClickObservable() | |
.subscribe(marker -> { | |
L.d("Info clicked: " + marker.getPosition()); | |
})); | |
} | |
private void initMarkers() { | |
ClusterManager<ClusterMarkerLocation> clusterManager = new ClusterManager<>(this, googleMap); | |
googleMap.setOnCameraChangeListener(clusterManager); | |
double lat, lng; | |
Random generator = new Random(); | |
for (int i = 0; i < 1000; i++) { | |
lat = generator.nextDouble() / 3; | |
lng = generator.nextDouble() / 3; | |
if (generator.nextBoolean()) { | |
lat = -lat; | |
} | |
if (generator.nextBoolean()) { | |
lng = -lng; | |
} | |
clusterManager.addItem(new ClusterMarkerLocation(new LatLng(mCenterLocation.latitude + lat, mCenterLocation.longitude + lng))); | |
} | |
} | |
private void initGoogleApiClient() { | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
private void initFragment() { | |
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.main_map); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
mGoogleApiClient.connect(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
subscriptions.unsubscribe(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { | |
mGoogleApiClient.disconnect(); | |
} | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
RxPermissions.getInstance(this) | |
.request(Manifest.permission.ACCESS_FINE_LOCATION) | |
.subscribe(granted -> { | |
if (granted) { | |
mCurrentLocation = LocationServices | |
.FusedLocationApi.getLastLocation(mGoogleApiClient); | |
initMapSettings(mCurrentLocation); | |
} else { | |
T.show(this, "Permission denied"); | |
} | |
}); | |
} | |
private void initMapSettings(Location mCurrentLocation) { | |
mCenterLocation = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); | |
final CameraPosition cameraPosition = CameraPosition.builder() | |
.target(mCenterLocation) | |
.zoom(16f) | |
.bearing(0.0f) | |
.tilt(0.0f) | |
.build(); | |
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); | |
googleMap.setMapType(MAP_TYPES[curMapTypeIndex]); | |
googleMap.setTrafficEnabled(true); | |
googleMap.setMyLocationEnabled(true); | |
googleMap.getUiSettings().setZoomControlsEnabled(true); | |
final CircleOptions circleOption = new CircleOptions(); | |
circleOption.center(mCenterLocation); | |
circleOption.radius(500); | |
circleOption.fillColor(ContextCompat.getColor(context, R.color.md_red_100)); | |
circleOption.strokeColor(ContextCompat.getColor(context, R.color.md_red_200)); | |
circleOption.strokeWidth(10); | |
googleMap.addCircle(circleOption); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment