Custom icon image on google map polyline - google-maps

Need to add Custom icon image on google map poly line, i tried using to add custom icon like marker it's not working.
This is the code i have tried but not working.
var lineSymbol = {
anchor: new google.maps.Point(0, 32),
origin: new google.maps.Point(0, 0),
scaledSize: new google.maps.Size(32, 32),
size: new google.maps.Size(64, 64),
url: "http://www.developerdrive.com/wp-content/uploads/2013/08/ddrive.png"
};
var Line = new google.maps.Polyline({
path: path,
geodesic: true,
strokeColor: "#35495e",
strokeOpacity: 0.8,
strokeWeight: 4,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
});

That method only works with SVG Symbols. In this case, just use a google.maps.Marker at the end of the path.
var lineSymbol = new google.maps.Marker({
icon: {
anchor: new google.maps.Point(0, 32),
origin: new google.maps.Point(0, 0),
scaledSize: new google.maps.Size(32, 32),
size: new google.maps.Size(64, 64),
url: "http://www.developerdrive.com/wp-content/uploads/2013/08/ddrive.png"
},
position: path[path.length-1],
map: map
});
proof of concept fiddle
code snippet:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: 0,
lng: -180
},
mapTypeId: 'terrain'
});
var path = [{lat: 37.772,lng: -122.214},
{lat: 21.291,lng: -157.821},
{lat: -18.142,lng: 178.431},
{lat: -27.467,lng: 153.027}
];
var lineSymbol = new google.maps.Marker({
icon: {
anchor: new google.maps.Point(16, 16), // center icon on end of polyline
origin: new google.maps.Point(0, 0),
scaledSize: new google.maps.Size(32, 32),
size: new google.maps.Size(64, 64),
url: "https://i.stack.imgur.com/7Fzjf.png"
},
position: path[path.length - 1],
map: map
});
var Line = new google.maps.Polyline({
path: path,
geodesic: true,
strokeColor: "#35495e",
strokeOpacity: 0.8,
strokeWeight: 4,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
});
Line.setMap(map);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < Line.getPath().getLength(); i++) {
bounds.extend(Line.getPath().getAt(i));
}
map.fitBounds(bounds);
}
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>

Related

Google Maps V3 Label alignment

I've created a custom marker in google maps and want to set labels on it (1 & 2), but somehow I cant position it and it always positions wrong:
(source: bilder-upload.eu)
I've already tried setting different anchors, but it seems like they wont move
var t = this,
markerConfig = {
lat: location.data('lat'),
lng: location.data('lng'),
label: location.data('id').toString(), color: 'white'
},
iconSize = 0.45,
icon = {
path: "M53.1,48.1c3.9-5.1,6.3-11.3,6.3-18.2C59.4,13.7,46.2,0.5,30,0.5C13.8,0.5,0.6,13.7,0.6,29.9 c0,6.9,2.5,13.1,6.3,18.2C12.8,55.8,30,77.5,30,77.5S47.2,55.8,53.1,48.1z",
fillColor: '#00492C',
fillOpacity: 1,
strokeWeight: 0,
scale: iconSize,
anchor: new google.maps.Point(38, 80),
};
t.marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(markerConfig.lat, markerConfig.lng),
label: {
text: markerConfig.label,
color: 'red',
},
icon: icon,
})
};
I expect it to align to the center of the markers.
To change the position of the label with respect to the marker, use labelOrigin:
labelOrigin
Type: Point
The origin of the label relative to the top-left corner of the icon image, if a label is supplied by the marker. By default, the origin is located in the center point of the image.
related questions:
How to position the label inside the Marker in Google Map?
Google Maps API, add custom SVG marker with label
Google map label placement
labelOrigin: new google.maps.Point(30, 30) works with your marker (and it looks like the anchor is slightly off, anchor: new google.maps.Point(32, 80), seems to work best.
var markerConfig = {
lat: 12.97,
lng: 77.59,
label: "1",
color: 'white'
},
iconSize = 0.45,
icon = {
path: "M53.1,48.1c3.9-5.1,6.3-11.3,6.3-18.2C59.4,13.7,46.2,0.5,30,0.5C13.8,0.5,0.6,13.7,0.6,29.9 c0,6.9,2.5,13.1,6.3,18.2C12.8,55.8,30,77.5,30,77.5S47.2,55.8,53.1,48.1z",
fillColor: '#00492C',
fillOpacity: 1,
strokeWeight: 0,
scale: iconSize,
anchor: new google.maps.Point(32, 80),
labelOrigin: new google.maps.Point(30, 30)
};
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(markerConfig.lat, markerConfig.lng),
label: {
text: markerConfig.label,
color: 'red',
},
icon: icon,
});
proof of concept fiddle
code snippet:
function initialize() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {
lat: 12.97,
lng: 77.59
}
});
var markerConfig = {
lat: 12.97,
lng: 77.59,
label: "1",
color: 'white'
},
iconSize = 0.45,
icon = {
path: "M53.1,48.1c3.9-5.1,6.3-11.3,6.3-18.2C59.4,13.7,46.2,0.5,30,0.5C13.8,0.5,0.6,13.7,0.6,29.9 c0,6.9,2.5,13.1,6.3,18.2C12.8,55.8,30,77.5,30,77.5S47.2,55.8,53.1,48.1z",
fillColor: '#00492C',
fillOpacity: 1,
strokeWeight: 0,
scale: iconSize,
anchor: new google.maps.Point(32, 80),
labelOrigin: new google.maps.Point(30, 30)
};
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(markerConfig.lat, markerConfig.lng),
label: {
text: markerConfig.label,
color: 'red',
},
icon: icon,
});
};
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

Custom image icon not positioning

I know this sounds duplicate but when I searched here in SO it doesn't help me. my problem is that I have custom icon or image the dimension is 256 x 256, scaledSize this to 50, now the problem is that it does not properly positioning to the latlng. also the problem is that when I zoom the map the marker will move or it will go away to it's position... unlike this example it work's fine
https://developers.google.com/maps/documentation/javascript/examples/icon-complex .
Here is my code
var imageicon= {
url: '/image/' + filename, // url
scaledSize: new google.maps.Size(50, 50), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 50) // anchor
};
custommarker= new google.maps.Marker({
flat: true,
icon: imageicon,
map: map,
optimized: false,
position: coordinate,
visible: true
});
Here is my marker
Your icon is 50px by 50px. You currently have the anchor set to (0,50) (the bottom left corner of the icon). You want the anchor set to the "point" at the bottom center (25,50)
var imageicon = {
url: 'https://i.stack.imgur.com/kEvED.png', // url
scaledSize: new google.maps.Size(50, 50), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(25, 50) // anchor
};
proof of concept fiddle
code snippet:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { // New York, NY
lat: 40.7127753,
lng: -74.0059728
},
zoom: 7
});
var imageicon = {
url: 'https://i.stack.imgur.com/kEvED.png', // url
scaledSize: new google.maps.Size(50, 50), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(25, 50) // anchor
};
var custommarker = new google.maps.Marker({
flat: true,
icon: imageicon,
map: map,
optimized: false,
position: map.getCenter(),
visible: true
});
}
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk">
</script>

Google Maps - Animated Polyline -- Stop / Remove Animation

I'm plotting a polyline on Google Maps API V3, from a GPX file.
On mouseover of that polyline, I have an animated dot, moving along the polyline, using function animateRoute();
Currently however, I don't have a way to remove the animated dot on mouseout, and as a result, if you mouseover, mouseout, mouseover etc, you end up with multiple animated dots moving along the same line.
Code snippet: (see full working URL below too)
var gmarkers = [];
function loadGPXFileIntoGoogleMap(map, filename,recordNum, name, hex_code) {
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: function(xml) {
var points = [];
var bounds = new google.maps.LatLngBounds ();
$(xml).find("trkpt").each(function() {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
if((lat != 0) && (lon != 0))
{
var p = new google.maps.LatLng(lat, lon);
points.push(p);
bounds.extend(p);
}
});
var strokeColor = "#ff0000";
var poly = new google.maps.Polyline({
path: points,
strokeColor: strokeColor,
strokeOpacity: 1,
strokeWeight: 4,
recordNum: recordNum,
});
poly.setMap(map);
google.maps.event.addListener(poly, 'mouseover', function() {
var start = {
path: "M-20,0a20,20 0 1,0 40,0a20,20 0 1,0 -40,0",
fillColor: '#00ff00',
fillOpacity: 1,
strokeColor:'#000000',
strokeWeight: 4,
scale: 0.5
}
var end = {
path: "M-20,0a20,20 0 1,0 40,0a20,20 0 1,0 -40,0",
fillColor: '#FF0000',
fillOpacity: 1,
strokeColor:'#000000',
strokeWeight: 4,
scale: 0.5
}
var markerStart = new google.maps.Marker({
position: poly.getPath().getAt(0),
icon: start,
map: map,
zIndex: 200,
scale: 1
});
gmarkers.push(markerStart);
var markerEnd = new google.maps.Marker({
position: poly.getPath().getAt(poly.getPath().getLength() - 1),
icon: end,
map: map,
zIndex: 200,
scale: 1
});
gmarkers.push(markerEnd);
var icons = this.setOptions({
icons: [{
icon: {
path: google.maps.SymbolPath.CIRCLE,
strokeOpacity: 1,
strokeColor: "#000000",
strokeWeight: 2,
scale: 4
},
}]});
animateRoute(poly);
});
function animateRoute(line) {
var count = 0;
window.setInterval(function() {
count = (count + 1) % 200;
var icons = poly.get('icons');
icons[0].offset = (count / 2) + '%';
poly.set('icons', icons);
}, 60);
}
google.maps.event.addListener(poly, 'mouseout', function() {
removeMarkers();
});
// fit bounds to track
map.fitBounds(bounds);
}
});
}
function removeMarkers(){
for(i=0; i<gmarkers.length; i++){
gmarkers[i].setMap(null);
}
}
$(document).ready(function() {
var mapOptions = {
zoom: 17,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map"),
mapOptions);
loadGPXFileIntoGoogleMap(map, "cmsAdmin/uploads/blue_and_green_not_comfortable_.gpx","724","Example A","FFFF00");
loadGPXFileIntoGoogleMap(map, "cmsAdmin/uploads/taraweratrailrouterecce.gpx","431","Example B","4F4CBE");
});
Full working example:
https://www.wildthings.club/mapStack.php
Hover over the blue line and you'll see the animated dot.
Mouse off, and then after a few seconds hover again - a second dot will appear, and the first dot is still going.
Repeat and you'll soon have a bunch of jittery dots.
Ideally I'd like to remove all animated dots on mouseout.
Second option would be to not add a subsequent animated dot icon if there is already one on that polyLine (note there are multiple polyLines on the map).
Third option failing that would be to have the animated dot stop and remove once it reaches the end (position markerEnd) so at least it doesn't loop.
I have tried placing the icons into an array and then removing from there (like I have done with the gmarkers array and removeMarkers(), but no luck.
I also had a play with Animate google maps polyline but this just works with straight line point to point, rather than following a series of points from a GPX file.
Any help, most appreciated
Cheers
You should use the window.clearInterval() function to remove the interval you are using to animate the icon on the polyline. You should save the id when call window.setInterval() in animateRoute(). Here is a simple JSBin proof of concept adapted from the code on that website. In my code, I'm just simply using a global id variable, and updating that variable in animateRoute():
<!DOCTYPE html>
<html>
<head>
<title>Polyline path</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map {
height: 100%;
width: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var map;
var id;
var gmarkers = [];
var gmarkersicons = [];
function initMap() {
var mapOptions = {
zoom: 3,
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: {lat: 9.291, lng: -157.821}
};
map = new google.maps.Map(document.getElementById("map"),
mapOptions);
var points = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
var poly = new google.maps.Polyline({
path: points,
strokeColor: "red",
strokeOpacity: 1,
strokeWeight: 4,
recordNum: "test"
});
poly.setMap(map);
google.maps.event.addListener(poly, 'mouseover', function() {
var start = {
path: "M-20,0a20,20 0 1,0 40,0a20,20 0 1,0 -40,0",
fillColor: '#00ff00',
fillOpacity: 1,
strokeColor:'#000000',
strokeWeight: 4,
scale: 0.5
}
var end = {
path: "M-20,0a20,20 0 1,0 40,0a20,20 0 1,0 -40,0",
fillColor: '#FF0000',
fillOpacity: 1,
strokeColor:'#000000',
strokeWeight: 4,
scale: 0.5
}
var go = {
path: "M-20,0a20,20 0 1,0 40,0a20,20 0 1,0 -40,0",
fillColor: '#000000',
fillOpacity: 1,
strokeColor:'#fff',
strokeWeight: 4,
scale: 0.5
}
var markerStart = new google.maps.Marker({
position: poly.getPath().getAt(0),
icon: start,
map: map,
zIndex: 200,
scale: 1
});
gmarkers.push(markerStart);
var markerEnd = new google.maps.Marker({
position: poly.getPath().getAt(poly.getPath().getLength() - 1),
icon: end,
map: map,
zIndex: 200,
scale: 1
});
gmarkers.push(markerEnd);
var icons = this.setOptions({
icons: [{
icon: {
path: google.maps.SymbolPath.CIRCLE,
strokeOpacity: 1,
strokeColor: "#000000",
strokeWeight: 2,
scale: 4
},
}]});
this.setOptions({
strokeColor: "red",
scale: 1,
strokeWeight:15,
strokeOpacity:.6
});
var contentString = "Testing";
var infowindow = new google.maps.InfoWindow({
content: contentString
});
infowindow.open(map, markerStart);
id = animateRoute(poly);
});
function animateRoute(line) {
var count = 0;
var id = window.setInterval(function() {
count = (count + 1) % 200;
var icons = poly.get('icons');
icons[0].offset = (count / 2) + '%';
poly.set('icons', icons);
}, 60);
return id;
}
google.maps.event.addListener(poly, 'mouseout', function() {
removeMarkers();
this.setOptions({strokeColor:"red",strokeWeight:4,strokeOpacity:1});
this.setOptions( { suppressMarkers: true } );
this.setOptions({
icons: [{}]});
window.clearInterval(id);
});
function removeMarkers(){
for(i=0; i<gmarkers.length; i++){
gmarkers[i].setMap(null);
}
}
}
$(document).ready(function() {
initMap();
});
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>

Drawing a flag on Google Maps using Drawing Manager doesn't give exact location

I'm using Google Maps drawing manager, when I draw an icon (flag) it doesn't show on the map as expected, I expect to see the flag exactly in the same location of the mouse click, but in my case it is far, any idea why?
Drawing Manager settings:
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingMode: null,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
}
});
See the description of Custom Markers in the documentation.
If you look at Google's example that uses that icon, you will see the correct parameters to put the "staff" of the flag at the defined coordinates:
// Origins, anchor positions and coordinates of the marker increase in the X
// direction to the right and in the Y direction down.
var image = {
url: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
// This marker is 20 pixels wide by 32 pixels high.
size: new google.maps.Size(20, 32),
// The origin for this image is (0, 0).
origin: new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at (0, 32).
anchor: new google.maps.Point(0, 32)
};
Use it in the marker creation like this:
var marker = new google.maps.Marker({
position: {lat: beach[1], lng: beach[2]},
map: map,
icon: image,
shape: shape,
title: beach[0],
zIndex: beach[3]
});
Or in the MarkerOptions property of the DrawingManager like this:
markerOptions: {
icon: {
url: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
// This marker is 20 pixels wide by 32 pixels high.
size: new google.maps.Size(20, 32),
// The origin for this image is (0, 0).
origin: new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at (0, 32).
anchor: new google.maps.Point(0, 32)
}
},
proof of concept fiddle (based on Google's DrawingManager example)
code snippet (based on Google's DrawingManager example):
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 8
});
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
},
markerOptions: {
icon: {
url: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
// This marker is 20 pixels wide by 32 pixels high.
size: new google.maps.Size(20, 32),
// The origin for this image is (0, 0).
origin: new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at (0, 32).
anchor: new google.maps.Point(0, 32)
}
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 1,
strokeWeight: 5,
clickable: false,
editable: true,
zIndex: 1
}
});
drawingManager.setMap(map);
}
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&callback=initMap" async defer></script>

Calculate the center of a polygon. google maps

I have this code where when the user clicks on the map like 3 times a polygon is created as well as a marker in each nodal point. Anyone knows how I could calculate the middle point of the polygon and drop a marker there as well ? thanks. here is my code.
var poly;
var map;
function initialize()
{
map = new google.maps.Map(document.getElementById('map'),
{
zoom: 14,
center: new google.maps.LatLng(12.303022,76.644917),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
poly = new google.maps.Polygon(
{
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#000000",
fillOpacity: 0.26
});
poly.setMap(map);
google.maps.event.addListener(map, 'click', addLatLng);
}
function addLatLng(event)
{
var path = poly.getPath();
path.push(event.latLng);
var marker = new google.maps.Marker(
{
position: event.latLng,
title: '#' + path.getLength(),
map: map
});
}