Reputation: 10896
I want to show a user image on the google map marker but I don't know how to do it?
Check the result I want here:
Marker Image:
The Result I want:
Thanks a lot
Upvotes: 5
Views: 8007
Reputation: 325
I can suggest some solution, there is some nice library https://www.npmjs.com/package/merge-images , so generally when you run over yours markers array you merge 2 images or more to base 64. here is code snippet:
getMarkerImage(elementMarker, fullArray) {
mergeImages([
{ src: './assets/img/markers/map-marker64.png', x: 0, y: 0 },
{ src: elementMarker.info.image || './assets/img/markers/markerIcon42.png', x: 11, y: 4 },
])
.then(imgData => {
console.log(imgData);
this.markersImages.push(imgData);
if (this.markersImages.length === this.controllersfullArray - 4) {
fullArray.forEach((element, i) => {
this.markers.push({
lat: element.info.location.latitude,
lng: element.info.location.longitude,
controllerInfo: element.info,
icon: {
url: this.markersImages[i],
labelOrigin: { x: 32, y: -10 },
scaledSize: {
width: 64,
height: 64
}
},
controllerStaus: element.status,
label: {
text: element.info.siteName,
color: "#ED2C2C",
fontWeight: "500",
fontSize: "14px"
}
});
console.log(this.markers);
});
}
});
// return (mergeImages({ src: './assets/img/markers/map-marker64.png', x: 0, y: 0 },
}
but be aware you need to use all images that are on the same Domain. because if not toDataUrl will throw an error of cross-origin
Upvotes: -1
Reputation: 10896
Solution Repo: https://github.com/amorenew/Flutter_avatar_maps
I used this library:
image: ^2.1.4
My main.dart
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as Images;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
Future<List<int>> makeReceiptImage() async {
// load avatar image
ByteData imageData = await rootBundle.load('assets/av.png');
List<int> bytes = Uint8List.view(imageData.buffer);
var avatarImage = Images.decodeImage(bytes);
//load marker image
imageData = await rootBundle.load('assets/ma.png');
bytes = Uint8List.view(imageData.buffer);
var markerImage = Images.decodeImage(bytes);
//resize the avatar image to fit inside the marker image
avatarImage = Images.copyResize(avatarImage,
width: markerImage.width ~/ 1.1, height: markerImage.height ~/ 1.4);
var radius = 90;
int originX = avatarImage.width ~/ 2, originY = avatarImage.height ~/ 2;
//draw the avatar image cropped as a circle inside the marker image
for (int y = -radius; y <= radius; y++)
for (int x = -radius; x <= radius; x++)
if (x * x + y * y <= radius * radius)
markerImage.setPixelSafe(originX + x+8, originY + y+10,
avatarImage.getPixelSafe(originX + x, originY + y));
return Images.encodePng(markerImage);
}
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<int> myImage;
void _incrementCounter() {
widget.makeReceiptImage().then((List<int> image) {
setState(() {
myImage = image;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.brown.shade100,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
myImage == null ? Text('fdassd') : Image.memory(myImage),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 1
Reputation: 622
If you are going to use local asset its an easy way to do that like @CopsOnRoad said, but, if you will load from network i will recommend this by the moment Flutter map inspire in Leaflet i have been using for a while know if work pretty fine.
Upvotes: 2
Reputation: 267474
As of now, you can't really use Widget
for Marker
, but here is what you can try, (haven't tested it personally)
var image = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(),
"assets/images/your_image.jpg",
);
Marker marker = Marker(
markerId: MarkerId("id"),
icon: image,
);
Upvotes: 0