How do I add custom marker images to Google Maps using the GWT Maps API? - gwt

I'm working on a GWT app that's using Google Maps. I'm trying to add many lettered markers to my map. Originally, I had:
Marker marker = new Marker(point);
marker.setImage("http://www.google.com/mapfiles/markerA.png");
map.addOverlay(marker);
But that didn't work. The call to setImage caused an exception in the maps API and nothing showed up on the map. I searched and found some half-answers talking about MarkerOptions, so I tried:
Icon icon = Icon.newInstance(Icon.DEFAULT_ICON);
icon.setImageURL("http://www.google.com/mapfiles/markerA.png");
MarkerOptions ops = MarkerOptions.newInstance(icon);
ops.setIcon(icon);
Marker marker = new Marker(point, ops);
map.addOverlay(marker);
This was a bit better in that my app was not throwing exceptions anymore and I was seeing marker shadows, but still no custom marker images.
I would prefer a non-JSNI solution to this problem.
Thanks!

This sample seems to cover what you want to achieve: IconDemo.java.
// Create our "tiny" marker icon
Icon icon = Icon.newInstance(
"http://labs.google.com/ridefinder/images/mm_20_red.png");
icon.setShadowURL("http://labs.google.com/ridefinder/images/mm_20_shadow.png");
icon.setIconSize(Size.newInstance(12, 20));
icon.setShadowSize(Size.newInstance(22, 20));
icon.setIconAnchor(Point.newInstance(6, 20));
icon.setInfoWindowAnchor(Point.newInstance(5, 1));
MarkerOptions options = MarkerOptions.newInstance();
options.setIcon(icon);
// LatLng point; MapWidget map;
map.addOverlay(new Marker(point, options));
The live demo can be seen here: http://gwt.google.com/samples/HelloMaps-1.0.4/HelloMaps.html#Creating%20Icons

Here's my new working solution (thx again igro):
Icon icon = Icon.newInstance("http://www.google.com/mapfiles/markerA.png");
icon.setIconSize(Size.newInstance(20, 34));
MarkerOptions ops = MarkerOptions.newInstance(icon);
Marker marker = new Marker(point, ops);
map.addOverlay(marker);

Here is my code in 3.10 Version
LatLng centerIcon = LatLng.newInstance(-25.90307367246304, -48.82550597190857);
MarkerImage markerImage = MarkerImage.newInstance("http://someIcon.png");
MarkerOptions mOpts = MarkerOptions.newInstance();
mOpts.setIcon(markerImage);
mOpts.setPosition(centerIcon);
Marker marker = Marker.newInstance(mOpts);
marker.setMap(mapWidget);

Related

Flutter google map web marker on click

I am using this library in my app to load the Google map on the web using this answer as a reference, I am able to add the marker on it but wondering how I can do the marker on tap action
Adding marker:
final _icon = jsMap.Icon()
..scaledSize = jsMap.Size(60, 60)
..url = 'https://www.freeiconspng.com/thumbs/pin-png/pin-png-9.png';
final marker = jsMap.Marker(
jsMap.MarkerOptions()
..position = latLng1
..map = map
..title = 'Test Title1'
..icon = _icon
);
I am able to do the on click action using the below code:
final infoWindow =
jsMap.InfoWindow(jsMap.InfoWindowOptions()..content = 'This is the content inside the Info window');
marker.onClick.listen((event) => infoWindow.open(map, marker));
It may help someone!

Mapbox GL-JS : How do I add a custom image for an icon instance?

I added a basic icon instance to my map, but cannot figure out how to give it a custom image. (I closely followed this example . After some digging, I have found examples on Mapbox's own page with code showing how to add custom images but it looks like they are adding markers as layers (with GeoJSON sources). It seems like so much code just to have a custom image. My code just looks like this :
var marker = new mapboxgl.Marker()
.setLngLat([-99, 30])
.addTo(map);
Is there a way to add my own image (PNG) for example? I have searched though the options for marker instances at the Mapbox documentation at this link but I can't find any information there.
Just try this...
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(https://path_to_your_icon/icon.png)';
el.style.width = '50px';
el.style.height = '50px';
new mapboxgl.Marker(el)
.setLngLat([-99, 30])
.addTo(map);

How do I remove a Leaflet polyline using marker coords?

I've placed my markers and drawn the polylines between them and it's working great.
I've also given the user the ability to remove a marker using the following function
function hide(marker) {
map.closePopup();
map.removeLayer(marker);
}
Now, when a marker is removed I'd also like to remove the polyline. I've been doing a lot of searching but haven't come across my specific issue: I'm using pixel coordinates and need to remove the polyline between two markers.
markers
var marker1 = L.marker(map.unproject([8706, 7789], map.getMaxZoom()));
var marker2 = L.marker(map.unproject([8302, 5273], map.getMaxZoom()));
var marker3 = L.marker(map.unproject([9303, 7251], map.getMaxZoom()));
polylines
polyline = L.polyline([
map.unproject([8706, 7789], map.getMaxZoom()),
map.unproject([8302, 5273], map.getMaxZoom()),
map.unproject([9303, 7251], map.getMaxZoom())
]);
So when a user removes marker1, the polyline disappears between marker1 and marker2, but remains between marker2 and marker3, and so on down the line...
How is this accomplished?
You can add the polylines to the markers. And if the marker is removed you can read out the lines and remove them too.
marker1 = L.marker([51.498912, -0.122223],{connectedLines: []}).addTo(mymap);
marker2 = L.marker([51.496988, -0.056305],{connectedLines: []}).addTo(mymap);
poly1 = L.polyline([marker1.getLatLng(),marker2.getLatLng()]).addTo(mymap);
marker1.options.connectedLines.push(poly1);
marker2.options.connectedLines.push(poly1);
function removeMarkerLine(e){
var marker = this;
if(marker.options && marker.options.connectedLines){
var lines = marker.options.connectedLines;
lines.forEach(function(line){
mymap.removeLayer(line);
});
}
mymap.removeLayer(marker);
};
https://jsfiddle.net/falkedesign/3aukgm7t/
Simplest way, keep track of which polylines belongs to which two markers, and if one is removed you also remove that one.
Can be easily accomplished using layer groups.

How can I position a marker on a map using GeoLocation?

I am creating a map page with a single image,
and I would like to use GeoLocation to show where the user is as a blue dot (somewhat like iOS maps).
I already have the JavaScript for finding the location, and it works great.
I just need help with how to place a marker - just a simple image - on the page according to the output of position.coords.latitude and position.coords.longitude.
Is there any way that this can be done, and if so, how?
Thanks in advance.
You can simply use GMarker in your javascript, something like this:
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
You can customize its image like that:
var image = 'beachflag.png';
marker.icon = image;

Bing maps ajax control 7.0 polygon fillColor in IE8

In internet explorer 8, none of my polygons on my bing maps have the proper fill color. They are always filled in white. I have no issues in firefox, chrome, safari or opera.
I've even tried using the exact code from the bing docs
// Initialize the map
var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {credentials:"Bing Maps Key"});
// Create the locations
var location1 = new Microsoft.Maps.Location(20,-20);
var location2 = new Microsoft.Maps.Location(20,20);
var location3 = new Microsoft.Maps.Location(-20,20);
var location4 = new Microsoft.Maps.Location(-20,-20);
// Create a polygon
var vertices = new Array(location1, location2, location3, location4, location1);
var polygoncolor = new Microsoft.Maps.Color(100,100,0,100);
var polygon = new Microsoft.Maps.Polygon(vertices,{fillColor: polygoncolor, strokeColor: polygoncolor});
// Add the shape to the map
map.entities.push(polygon)
// Set the view
map.setView({bounds: Microsoft.Maps.LocationRect.fromLocations(vertices)});
Has anyone experienced this?
I am also using jquery 1.5.1
Figured out the issue.
I was using a htc file (ie-css3.htc) for some css3 effects in IE. I guess this doesn't play nicely with bing maps polygon colors.