This library mirrors the complete Google Maps API v2 for Android using only interfaces. This enables you to write maps code once and use it with all compatible maps APIs. Currently there are bindings for Google Maps v2 and Amazon Maps v2.
There are two approaches for integrating maps libraries:
- Fat apk: Single apk containing both maps libraries
- Thin apk: Multiple apks, containing only a single maps library (recommended)
Add the following to your Gradle build file
dependencies {
compile 'de.quist.app.maps:google-maps-wrapper:1.0+'
compile 'de.quist.app.maps:amazon-maps-wrapper:1.0+'
}
Now you have to decide on runtime which library to use. This is typically done in an early stage, e.g. in your Application subclass. Depending on which library is available, either use de.quist.app.maps.google.GoogleMapBinding.INSTANCE
(for Google Maps Library) or de.quist.app.maps.amazon.AmazonMapBinding.INSTANCE
(for Amazon Maps Library) for creating maps objects.
This is the preferred approach and is implemented in the demo project. Create two flavors of your app and specify a Maps binding in your BuildConfig class:
android {
...
productFlavors {
play {
buildConfigField "de.quist.app.maps.MapBinding", "MAP_BINDING", "de.quist.app.maps.google.GoogleMapBinding.INSTANCE"
}
amazon {
minSdkVersion 15
buildConfigField "de.quist.app.maps.MapBinding", "MAP_BINDING", "de.quist.app.maps.amazon.AmazonMapBinding.INSTANCE"
}
}
}
dependencies {
playCompile 'de.quist.app.maps:google-maps-wrapper:1.0+'
amazonCompile 'de.quist.app.maps:amazon-maps-wrapper:1.0+'
}
- Rename Google–specific namespaces and classes to the abstraction API versions as described below.
- Change all instantiation of all
*Options
classes by calling the corresponding method on a MapBinding instance. E.g. creation of marker options (new MarkerOption()
) should be replace withbinding.markerOptions()
, assuming that variablebinding
contains either an instance ofde.quist.app.maps.amazon.AmazonMapBinding
orde.quist.app.maps.google.GoogleMapBinding
- Change calls to
*Factory
classes to the corresponding factory instance. E.g. a call toCameraUpdateFactory.zoomIn()
should be replaced bybinding.cameraUpdateFactory().zoomIn()
- For all layout files containing a fragment node with a class attribute
MapFragment
orSupportMapFragment
should provide an alternative layout, containing the corresponding Amazon map fragment. If you use the Light Apk Approach, provide a separate layout in the resources directory of the corresponding flavor (see demo app). - To create instances of
MapFragment
,SupportMapFragment
orMapVIew
programmatically, use thenewMapFragmentInstance()
,newSupportMapFragmentInstance()
andnewMapView()
methods inMapBinding
. - To access
MapFragment
orMapView
methods, wrap the fragment or view into aMapFragmentWrapper
orMapViewWrapper
by callingmapFragmentWrapperFrom()
ormapViewWrapperFrom()
.
Google Name | Abstraction Name | Example |
---|---|---|
com.google.android.gms.maps |
de.quist.app.maps |
com.google.android.gms.maps.Projection → de.quist.app.maps.Projection |
GoogleMap |
Map |
|
new *Options() |
binding.*Options() |
new MarkerOptions() → binding.markerOptions() |
new LatLng() |
binding.latLng() |
new LatLng(53.55, 10.0) → binding.latLng(53.55, 10.) |
*Factory.*() |
binding.*Factory().* |
CameraUpdateFactory.zoomIn() → binding.cameraUpdateFactory().zoomIn() |
MapsFragment.newInstance() |
binding.newMapFragmentInstance() |
MapsFragment.newInstance() → binding.newMapsFragmentInstance() |
Tile.NO_TILE |
binding.noTile() |
|
*.builder() |
binding.*Builder() |
CameraPosition.builder() → binding.cameraPositionBuilder() |