Back to project page Vispin.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. ...
If you think the Android project Vispin 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.jpardogo.vispin.ui; /* ww w.j a va 2 s .co m*/ import android.app.Activity; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.PopupWindow; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; 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.jpardogo.vispin.R; import com.jpardogo.vispin.adapters.PinInfoWindowAdapter; import java.io.IOException; import java.util.List; import java.util.Locale; import java.util.Objects; import rx.Observable; import rx.Observer; import rx.Subscriber; import rx.Subscription; import rx.android.observables.AndroidObservable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import timber.log.Timber; public class PinsMapFragment extends MapFragment implements GoogleMap.OnMapLongClickListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener { private GoogleMap mGoogleMap; private OnMapPinsInteractionListener mActivityCallback; private Subscription mSubscription; private Geocoder mGeocoder; @Override public void onAttach(Activity activity) { super.onAttach(activity); mGeocoder = new Geocoder(activity, Locale.getDefault()); try { mActivityCallback = (OnMapPinsInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement " + OnMapPinsInteractionListener.class.getSimpleName()); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = super.onCreateView(inflater, container, savedInstanceState); mGoogleMap = getMap(); mGoogleMap.setOnMapLongClickListener(this); mGoogleMap.setOnMarkerClickListener(this); mGoogleMap.setOnMapClickListener(this); return rootView; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.fragment_pinsmap, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public void onMapLongClick(LatLng latLng) { mSubscription = AndroidObservable.bindFragment(this, getAddresses(latLng)) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mAddressesObserver); } private Observer<List<Address>> mAddressesObserver = new Observer<List<Address>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Timber.e(e, "mAddressesObserver found an error"); } @Override public void onNext(List<Address> addresses) { try { Address address = addresses.get(addresses.size() - 1); mActivityCallback.startPinCreation(address); } catch (ArrayIndexOutOfBoundsException e) { Toast.makeText(getActivity(), "No country around here", Toast.LENGTH_SHORT).show(); } } }; public void drawMarker(MarkerOptions markerOptions) { Marker maker = mGoogleMap.addMarker(markerOptions); mActivityCallback.pinSelected(maker); mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(markerOptions.getPosition(), mGoogleMap.getCameraPosition().zoom),500,null); } private Observable<List<Address>> getAddresses(final LatLng latLng) { return Observable.create(new Observable.OnSubscribe<List<Address>>() { @Override public void call(Subscriber<? super List<Address>> subscriber) { try { List<Address> addresses = mGeocoder.getFromLocation(latLng.latitude, latLng.longitude, 100); subscriber.onNext(addresses); subscriber.onCompleted(); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void onDestroy() { super.onDestroy(); if (mSubscription != null) { mSubscription.unsubscribe(); } } @Override public boolean onMarkerClick(Marker marker) { mActivityCallback.pinSelected(marker); return false; } @Override public void onMapClick(LatLng latLng) { mActivityCallback.mapSelected(); } public interface OnMapPinsInteractionListener { void startPinCreation(Address address); void pinSelected(Marker marker); void mapSelected(); } }