Skip to content
This repository was archived by the owner on Jun 25, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 109 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ You can refer to the [example](https://github.com/apptreesoftware/flutter_google
- [X] Customize Pin color
- [X] Polyline support
- [X] Polygon support
- [X] Customize pin image
- [X] Remove markers, polylines & polygons.

### Upcoming
- [ ] Customize pin image
- [ ] Remove markers

- [ ] Bounds geometry functions

## Usage examples
Expand Down Expand Up @@ -192,6 +193,93 @@ mapView.setMarkers(<Marker>[
mapView.addMarker(new Marker("3", "10 Barrel", 45.5259467, -122.687747,
color: Colors.purple));
```

#### Edit custom Marker image
First add your assets to a folder in your project directory. The name of the folder could be any but "images" or "assets" are the more common.
It should look like this.

```dart
- project_name
|-android
|-images
|-flower_vase.png
|-ios
|-lib
# Rest of project folders and files
```

Then add asset to the pubspec.yaml under flutter tag.
```dart
flutter:
# Code already existent

# Added asset.
assets:
- images/flower_vase.png
```

Finally use the asset name as icon for your marker. If the width or height is not set or is equals to 0, the image original value of said attribute will be used.

```dart
new Marker(
"1",
"Something fragile!",
45.52480841512737,
-122.66201455146073,
color: Colors.blue,
draggable: true, //Allows the user to move the marker.
markerIcon: new MarkerIcon(
"images/flower_vase.png", //Asset to be used as icon
width: 112.0, //New width for the asset
height: 75.0, // New height for the asset
),
);
```
#### Set a Marker draggable and listening to position changes
First set the draggable attribute of a marker to true.
```dart
Marker marker=new Marker(
"1",
"Something fragile!",
45.52480841512737,
-122.66201455146073,
draggable: true, //Allows the user to move the marker.
);
```
Now add listeners for the events.
```dart
// This listener fires when the marker is long pressed and could be moved.
mapView.onAnnotationDragStart.listen((markerMap) {
var marker = markerMap.keys.first;
var location = markerMap[marker]; // The original location of the marker before moving it. Use it if needed.
print("Annotation ${marker.id} dragging started");
});
// This listener fires when the user releases the marker.
mapView.onAnnotationDragEnd.listen((markerMap) {
var marker = markerMap.keys.first;
var location = markerMap[marker]; // The actual position of the marker after finishing the dragging.
print("Annotation ${marker.id} dragging ended");
});
// This listener fires every time the marker changes position.
mapView.onAnnotationDrag.listen((markerMap) {
var marker = markerMap.keys.first;
var location = markerMap[marker]; // The updated position of the marker.
print("Annotation ${marker.id} moved to ${location.latitude} , ${location
.longitude}");
});
```

#### Add a single polyline to the map
```dart
mapView.addPolyline(new Polyline(
"12",
<Location>[
new Location(45.519698, -122.674932),
new Location(45.516687, -122.667014),
],
width: 15.0));
```

#### Add multiple polylines to the map
```dart
mapView.setPolylines(<Polyline>[
Expand All @@ -217,16 +305,23 @@ mapView.setPolylines(<Polyline>[
]);
```

#### Add a single polyline to the map
#### Add a single polygon to the map
```dart
mapView.addPolyline(new Polyline(
"12",
<Location>[
new Location(45.519698, -122.674932),
new Location(45.516687, -122.667014),
],
width: 15.0));
mapView.addPolygon(new Polygon(
"111",
<Location>[
new Location(45.5231233, -122.6733130),
new Location(45.5233225, -122.6732969),
new Location(45.5232398, -122.6733506),
new Location(45.5231233, -122.6733130),
],
jointType: FigureJointType.round,
strokeWidth: 5.0,
strokeColor: Colors.red,
fillColor: Color.fromARGB(75, 255, 0, 0),
));
```

#### Add multiple polygons to the map
```dart
mapView.setPolygons(<Polygon>[
Expand Down Expand Up @@ -268,22 +363,12 @@ mapView.addPolyline(new Polyline(
]);
```

#### Add a single polygon to the map
#### Remove elements from the map
```dart
mapView.addPolygon(new Polygon(
"111",
<Location>[
new Location(45.5231233, -122.6733130),
new Location(45.5233225, -122.6732969),
new Location(45.5232398, -122.6733506),
new Location(45.5231233, -122.6733130),
],
jointType: FigureJointType.round,
strokeWidth: 5.0,
strokeColor: Colors.red,
fillColor: Color.fromARGB(75, 255, 0, 0),
));
//Remove all markers
mapView.clearA
```

#### Zoom to fit all the pins on the map
```dart
mapView.zoomToFit(padding: 100);
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group 'com.apptreesoftware.mapview'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.2.41'
ext.kotlin_version = '1.2.50'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -45,6 +45,6 @@ android {

dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.41'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
}
90 changes: 66 additions & 24 deletions android/src/main/kotlin/com/apptreesoftware/mapview/MapActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.apptreesoftware.mapview
import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.content.res.AssetFileDescriptor
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
Expand All @@ -22,7 +25,6 @@ class MapActivity : AppCompatActivity(),
var polylineIdLookup = HashMap<String, Polyline>()
var polygonIdLookup = HashMap<String, Polygon>()
val PermissionRequest = 1
var tapCoordinate: LatLng? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -31,14 +33,16 @@ class MapActivity : AppCompatActivity(),
val mapFragment = supportFragmentManager.findFragmentById(
R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
if (MapViewPlugin.hideToolbar)
this.supportActionBar?.hide()
MapViewPlugin.mapActivity = this
}

@SuppressLint("MissingPermission")
override fun onMapReady(map: GoogleMap) {
googleMap = map
map.setMapType(MapViewPlugin.mapViewType)

map.uiSettings.isCompassEnabled = MapViewPlugin.showCompassButton
if (MapViewPlugin.showUserLocation) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Expand All @@ -47,11 +51,31 @@ class MapActivity : AppCompatActivity(),
ActivityCompat.requestPermissions(this, array, PermissionRequest)
} else {
map.isMyLocationEnabled = true
map.uiSettings.isMyLocationButtonEnabled = true
map.uiSettings.isIndoorLevelPickerEnabled = true
map.uiSettings.isMyLocationButtonEnabled = MapViewPlugin.showMyLocationButton
}
}
map.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
override fun onMarkerDragEnd(p0: Marker?) {
if (p0 != null) {
val id: String = p0.tag as String
MapViewPlugin.annotationDragEnd(id, p0.position)
}
}

override fun onMarkerDragStart(p0: Marker?) {
if (p0 != null) {
val id: String = p0.tag as String
MapViewPlugin.annotationDragStart(id, p0.position)
}
}

override fun onMarkerDrag(p0: Marker?) {
if (p0 != null) {
val id: String = p0.tag as String
MapViewPlugin.annotationDrag(id, p0.position)
}
}
})
map.setOnMapClickListener { latLng ->
MapViewPlugin.mapTapped(latLng)
}
Expand Down Expand Up @@ -109,9 +133,14 @@ class MapActivity : AppCompatActivity(),
get() = googleMap?.cameraPosition?.target ?: LatLng(0.0,
0.0)

fun setCamera(target: LatLng, zoom: Float) {
googleMap?.animateCamera(
CameraUpdateFactory.newLatLngZoom(target, zoom))
fun setCamera(target: LatLng, zoom: Float, bearing: Float, tilt: Float) {
val cameraPosition = CameraPosition.Builder()
.target(target)
.zoom(zoom)
.bearing(bearing)
.tilt(tilt)
.build()
googleMap?.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}

fun setAnnotations(annotations: List<MapAnnotation>) {
Expand Down Expand Up @@ -361,26 +390,39 @@ class MapActivity : AppCompatActivity(),
}

fun createMarkerForAnnotation(annotation: MapAnnotation, map: GoogleMap): Marker {
val marker: Marker
val markerOptions = MarkerOptions()
.position(annotation.coordinate)
.title(annotation.title)
.draggable(annotation.draggable)
.rotation(annotation.rotation.toFloat())
if (annotation is ClusterAnnotation) {
marker = map
.addMarker(MarkerOptions()
.position(annotation.coordinate)
.title(annotation.title)
.icon(
BitmapDescriptorFactory.defaultMarker(
annotation.colorHue)))
marker.tag = annotation.identifier
markerOptions.snippet(annotation.clusterCount.toString())
}
var bitmap: Bitmap? = null
if (annotation.icon != null) {
try {
val assetFileDescriptor: AssetFileDescriptor = MapViewPlugin.getAssetFileDecriptor(annotation.icon.asset)
val fd = assetFileDescriptor.createInputStream()
bitmap = BitmapFactory.decodeStream(fd)
var width = annotation.icon.width
var height = annotation.icon.height
if (width == 0.0)
width = bitmap.width.toDouble()
if (height == 0.0)
height = bitmap.height.toDouble()
bitmap = Bitmap.createScaledBitmap(bitmap, width.toInt(), height.toInt(), false)
} catch (exception: Exception) {
exception.printStackTrace()
}
}
if (bitmap != null) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
} else {
marker = map
.addMarker(MarkerOptions()
.position(annotation.coordinate)
.title(annotation.title)
.icon(
BitmapDescriptorFactory.defaultMarker(
annotation.colorHue)))
marker.tag = annotation.identifier
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(
annotation.colorHue))
}
val marker = map.addMarker(markerOptions)
marker.tag = annotation.identifier
return marker
}

Expand Down
Loading