Skip to content

Commit

Permalink
Added the setTouchEnabled method so we can disable any touch inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomafc330 committed Mar 7, 2016
1 parent 1ec03cc commit 0f372d3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
3 changes: 3 additions & 0 deletions library/src/main/assets/google_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
function stopTrackingUserLocation() {
GeoMarker.setMap(null);
}
function setTouchEnabled(enabled) {
map.setOptions({draggable: enabled, zoomControl: enabled, scrollwheel: enabled, disableDoubleClickZoom: !enabled});
}

function clearMarkers() {
for (var key in markers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public AirGoogleMapOptions mapToolbarEnabled(boolean enabled) {
return this;
}

public AirGoogleMapOptions setTouchEnabled(boolean enabled) {
tiltGesturesEnabled(enabled);
rotateGesturesEnabled(enabled);
zoomGesturesEnabled(enabled);
scrollGesturesEnabled(enabled);
return this;
}

public Boolean getZOrderOnTop() {
return options.getZOrderOnTop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,9 @@ public interface AirMapInterface {
* Remove GeoJson layer from map, if any.
*/
void clearGeoJsonLayer();
/**
* Set to whether to enable touch. If it's disabled then the user won't be allowed to drag, zoom or pan.
* @param enabled
*/
void setTouchEnabled(boolean enabled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ public void setMyLocationEnabled(boolean trackUserLocation) {
mapInterface.setMyLocationEnabled(trackUserLocation);
}

public void setTouchEnabled(boolean enabled) {
mapInterface.setTouchEnabled(enabled);
}

@Override public void onCameraChanged(LatLng latLng, int zoom) {
if (onCameraChangeListener != null) {
onCameraChangeListener.onCameraChanged(latLng, zoom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public void onMapReady(GoogleMap googleMap) {
if (onMapLoadedListener != null) {
onMapLoadedListener.onMapLoaded();
}

}
}
});
Expand Down Expand Up @@ -126,6 +127,10 @@ public void onInfoWindowClick(Marker marker) {
});
}

@Override public void setTouchEnabled(boolean enabled) {
googleMap.getUiSettings().setAllGesturesEnabled(enabled);
}

@Override public void setInfoWindowCreator(GoogleMap.InfoWindowAdapter adapter,
InfoWindowCreator creator) {
googleMap.setInfoWindowAdapter(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ public void setOnMarkerClickListener(OnMapMarkerClickListener listener) {
// no-op
}

@Override
public void setTouchEnabled(boolean enabled) {
webView.loadUrl("javascript:setTouchEnabled(" + enabled + ");");
}

@Override public <T> void addPolyline(AirMapPolyline<T> polyline) {
try {
JSONArray array = new JSONArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.widget.TextView;
import android.widget.Toast;

import com.airbnb.android.airmapview.AirGoogleMapOptions;
import com.airbnb.android.airmapview.AirMapGeoJsonLayer;
import com.airbnb.android.airmapview.AirMapInterface;
import com.airbnb.android.airmapview.AirMapMarker;
Expand All @@ -30,7 +31,9 @@
import com.airbnb.android.airmapview.listeners.OnLatLngScreenLocationCallback;
import com.airbnb.android.airmapview.listeners.OnMapClickListener;
import com.airbnb.android.airmapview.listeners.OnMapInitializedListener;
import com.airbnb.android.airmapview.listeners.OnMapLoadedListener;
import com.airbnb.android.airmapview.listeners.OnMapMarkerClickListener;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

Expand Down Expand Up @@ -122,6 +125,24 @@ public class MainActivity extends AppCompatActivity
case R.id.action_clear_logs:
textLogs.setText("");
break;
case R.id.action_web_map_no_touch:
try {
airMapInterface = createMapWithTouchDisabled(AirMapViewTypes.WEB);
} catch (UnsupportedOperationException e) {
Toast.makeText(this, "Sorry, native Google Maps are not supported by this device. " +
"Please make sure you have Google Play Services installed.",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.action_native_map_no_touch:
try {
airMapInterface = createMapWithTouchDisabled(AirMapViewTypes.NATIVE);
} catch (UnsupportedOperationException e) {
Toast.makeText(this, "Sorry, native Google Maps are not supported by this device. " +
"Please make sure you have Google Play Services installed.",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_geojson_layer:
// Draws a layer on top of Australia
String geoJsonString = Util.readFromRawResource(this, R.raw.google);
Expand Down Expand Up @@ -152,6 +173,25 @@ public class MainActivity extends AppCompatActivity
return super.onOptionsItemSelected(item);
}

private AirMapInterface createMapWithTouchDisabled(AirMapViewTypes type) {
if (type == AirMapViewTypes.NATIVE) {
AirGoogleMapOptions options = new AirGoogleMapOptions(new GoogleMapOptions());
options.setTouchEnabled(false);
AirMapInterface airMapInterface = mapViewBuilder.builder(type).withOptions(options).build();
return airMapInterface;
}

// case where it's not a native map, doesn't use AirGoogleMapOptions
final AirMapInterface mapInterface = mapViewBuilder.builder(type).build();
mapInterface.setOnMapLoadedListener(new OnMapLoadedListener() {
@Override
public void onMapLoaded() {
mapInterface.setTouchEnabled(false);
}
});
return mapInterface;
}

@Override public void onCameraChanged(LatLng latLng, int zoom) {
appendLog("Map onCameraChanged triggered with lat: " + latLng.latitude + ", lng: "
+ latLng.longitude);
Expand Down
17 changes: 13 additions & 4 deletions sample/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<item android:id="@+id/action_native_map"
android:title="Use Native Map"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="@+id/action_native_map"
android:title="Use Native Map"
android:orderInCategory="100"
app:showAsAction="never"/>

<item android:id="@+id/action_google_web_map"
android:title="Use Google Web Map"
Expand Down Expand Up @@ -38,5 +38,14 @@
android:orderInCategory="100"
app:showAsAction="never"/>

<item android:id="@+id/action_web_map_no_touch"
android:title="Use Web Map (Don't allow drag)"
android:orderInCategory="100"
app:showAsAction="never"/>

<item android:id="@+id/action_native_map_no_touch"
android:title="Use Native Map (Don't allow drag)"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>

0 comments on commit 0f372d3

Please sign in to comment.