I've created a simple map with custom PNG markers. Is it possible to offset the attached PNG image? There does not seem to be any mention of an 'offset' in the Google Map API v3 documentation.
As of v3.10, the MarkerImage class has been deprecated, the Icon anonymous object should be used instead. From the documentation
Until version 3.10 of the Google Maps JavaScript API, complex icons were defined as MarkerImage objects. The Icon object literal was added in version 3.10, and replaces MarkerImage from version 3.11 onwards.
For example:
var marker = new google.maps.Marker({
map:map,
position: map.getCenter(),
icon: {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
}
});
example fiddle
code snippet"
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(44.5403, -78.5463),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
var marker = new google.maps.Marker({
map: map,
position: map.getCenter(),
icon: {
url: "http://i.stack.imgur.com/PYAIJ.png",
size: new google.maps.Size(36, 36),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(18, 18),
scaledSize: new google.maps.Size(25, 25)
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
width: 100%;
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
The anchor option on the MarkerImage class lets offset the marker image from the middle center position on the marker's lat/lng:
'anchor' overrides the position of the
anchor point from its default bottom
middle position
I was looking for just this option and found a sample here:
http://econym.org.uk/gmap/custom.htm
Setting iconAnchor (on the marker data actually) worked for me.
var Icon = new GIcon();
Icon.image = "mymarker.png";
Icon.iconSize = new GSize(20, 34);
Icon.shadow = "myshadow.png";
Icon.shadowSize = new GSize(36, 34);
Icon.iconAnchor = new GPoint(5, 34);
Icon.infoWindowAnchor = new GPoint(5, 2);
Icon.transparent = "mytran.png";
For v3, this is how I accomplished it:
var image = new google.maps.MarkerImage("images/car1.png",
new google.maps.Size(60, 60),
new google.maps.Point(0,0),
new google.maps.Point(30, 30)
);
//ADD MARKER AT EACH POINT
var marker = new google.maps.Marker();
marker.setPosition(new_point);
marker.setIcon(image);
marker.setZIndex(0);
marker.setMap(map);
Related
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>
I am attempting to get a Google maps GroundOverlay working without luck. Searched everywhere, read the Google reference documentation, still no lucK.
What I have attempted can be seen at:
http://www.ryecemetery.com.au/locate.html
Very basic code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_SJakjgkzjvWoMu7T-DqsUDWUEbfUtTA"></script>
</head>
<body>
<div style="width: 1200px; height: 800px" id="map-canvas"></div>
<script>
var historicalOverlay;
var myLatlng = new google.maps.LatLng(-38.374058,144.822763);
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 18,
center: myLatlng
});
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-38.374615, 144.823766),
new google.maps.LatLng(-38.373628, 144.821631)
);
historicalOverlay = new google.maps.GroundOverlay(
'http://www.ryecemetery.com.au/images/layout-06a.jpg',
imageBounds);
historicalOverlay.setMap(map);
</script>
</body>
</html>
You can see that some sort of overlay is placed on the map. I believe I have the sw, ne latlng correct. If I check on Google maps by right clicking and choosing "whats here" those are the latlng that is given.
I have the overlay in png, gif and jpeg. With the png and gif I get nothing to appear.
Where have I gone wrong?
thanks
Ken
Your bounds is wrong for your groundOverlay.
according to the documentation for the constructor for a google.maps.LatLngBounds object:
Constructor
LatLngBounds(sw?:LatLng, ne?:LatLng) Constructs a rectangle from the points at its south-west and north-east corners.
The arguments should be SW, NE. Your arguments are SE, NW. Your code:
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-38.374615, 144.823766), //SE
new google.maps.LatLng(-38.373628, 144.821631) //NW
);
If I swap the longitudes between the two points it works:
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-38.374615, 144.821631), //SW
new google.maps.LatLng(-38.373628, 144.823766) //NE
);
proof of concept fiddle
code snippet:
var historicalOverlay;
var myLatlng = new google.maps.LatLng(-38.374058, 144.822763);
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 18,
center: myLatlng
});
var markerNW = new google.maps.Marker({
position: new google.maps.LatLng(-38.374615, 144.823766),
map: map,
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
},
title: "SW"
});
var markerSE = new google.maps.Marker({
position: new google.maps.LatLng(-38.373628, 144.821631),
map: map,
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
},
title: "NE"
});
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-38.374615, 144.821631),
new google.maps.LatLng(-38.373628, 144.823766));
historicalOverlay = new google.maps.GroundOverlay(
'http://www.ryecemetery.com.au/images/layout-06a.jpg',
imageBounds);
historicalOverlay.setMap(map);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>
I tried to instantiate google.maps.Icon,
var icon = new google.maps.Icon({
anchor:new google.maps.Point(0,0),
origin:new google.maps.Point(0,0),
scaledSize: new google.maps.Size(10,10),
size: new google.maps.Size(10,10),
url:"http://maps.google.com/mapfiles/kml/paddle/purple-square.png"
});
But I got the error,
Uncaught TypeError: undefined is not a function
I can't get this to work: What is the construct interface `google.maps.Icon` (Example)
For me it works with an anonymous object:
var image = {url: 'https://developers.google.com/maps/documentation/javascript/examples/images/beachflag.png',
size: new google.maps.Size(20, 32),
origin: new google.maps.Point(0,0),
anchor: new google.maps.Point(0, 32)};
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
});
Example from the documentation
Example with your icon
code snippet:
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(-33.9, 151.2),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
setMarkers(map, beaches);
}
/**
* Data for the markers consisting of a name, a LatLng and a zIndex for
* the order in which these markers should display on top of each
* other.
*/
var beaches = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
function setMarkers(map, locations) {
// Add markers to the map
// Marker sizes are expressed as a Size of X,Y
// where the origin of the image (0,0) is located
// in the top left of the image.
// Origins, anchor positions and coordinates of the marker
// increase in the X direction to the right and in
// the Y direction down.
var image = {
anchor: new google.maps.Point(16, 32),
origin: new google.maps.Point(0, 0),
scaledSize: new google.maps.Size(32, 32),
size: new google.maps.Size(64, 64),
url: "http://maps.google.com/mapfiles/kml/paddle/purple-square.png"
};
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
title: beach[0],
zIndex: Math.round(myLatLng.lat() * -100000) << 5
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map_canvas {
height: 100%;
}
#media print {
html,
body {
height: auto;
}
#map_canvas {
height: 650px;
}
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
google.maps.Icon is a Javascript object literal and not a class. I didn't know this till 5 minutes ago when I was trying to solve the same problem.
The google maps API docs:
https://developers.google.com/maps/documentation/javascript/overlays#ComplexIcons
give an example of converting from the deprecated MarkerImage (v3.9) to Icon (v3.10+)
var image = new google.maps.MarkerImage(
place.icon,
new google.maps.Size(71, 71),
new google.maps.Point(0, 0),
new google.maps.Point(17, 34),
new google.maps.Size(25, 25));
becomes
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
So Allen (and I) tried to use a constructor new google.maps.Icon() which doesn't exist. Just remove that text leaving var icon = {<list of properties>}; and everything should work.
The google API ref:
https://developers.google.com/maps/documentation/javascript/reference#Icon
doesn't make this very obvious.
I also went to educate myself about object literals - you can find the explanation at mozilla - search for javascript object literals - I can't post the link as this is the first answer I have given on stack exchange and I need more points to add more than two links!
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(53.385846,-1.471385),
map: map,
icon: { url:'images/markers.png', size:{height:37,width:32},origin:{x:32,y:0},anchor:{x:15,y:35}
},
draggable:true
});
I use this code to add marker to a Google map, but it doesn't work.
When I change the draggable parameter value from true to false, it works.
Is it a bug in Google Maps?
The definition of an Icon
defines it in terms of google.maps.Point and google.maps.Size objects, not anonymous javascript objects as in the code example. Note the Icon class is a replacement for the newly deprecated MarkerImage class.
anchor Point
origin Point
scaledSize Size
size Size
This might work better (not tested):
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(53.385846,-1.471385),
map: map,
icon: {
url:'images/markers.png',
size: new google.maps.Size(37,32},
origin: new google.maps.Point(32,0),
anchor: new google.maps.Point(15,35)
},
draggable:true
});
working example
code snippet:
var map;
var triangle = null;
var myLatLng = new google.maps.LatLng(53.385846, -1.471385);
function initialize() {
var mapOptions = {
center: myLatLng,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker1 = new google.maps.Marker({
position: myLatLng,
map: map,
icon: {
url: 'http://www.geocodezip.com/mapIcons/marker1.png',
size: new google.maps.Size(20, 34),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(10, 34)
},
draggable: true
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="height: 400px; width:500px;"></div>
I have gotten to the point of passing a value from set of Markers created via PHP but I can not figure out how to create or implement the IF condition to show the Marker Image related to the Type value.
Code:
<script type="text/javascript">
var iconStar = new google.maps.MarkerImage("googleMarkers/star.png",
new google.maps.Size(32, 28),
new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var iconBlue = new google.maps.MarkerImage("images/mm_20_blue.png",
new google.maps.Size(12, 20),
new google.maps.Point(0,0),
new google.maps.Point(6, 20));
var iconRed = new google.maps.MarkerImage("images/mm_20_red.png",
new google.maps.Size(12, 20),
new google.maps.Point(6, 20),
new google.maps.Point(5, 1));
var iconYellow = new google.maps.MarkerImage("images/mm_20_yellow.png",
new google.maps.Size(12, 20),
new google.maps.Point(6, 20),
new google.maps.Point(5, 1));
iconType = [] = iconStar;
iconType["0"] = iconStar;
iconType["1"] = iconBlue;
iconType["2"] = iconRed;
iconType["3"] = iconYellow;
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info, type) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: iconType,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
// panTo puts you back to the original center - not good for zoomed in nav
// map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
}
});
<?php
do {
$name=$row_rsCity['DEALER'];
$lat=$row_rsCity['lat'];
$lon=$row_rsCity['lng'];
$desc=$row_rsCity['ADDRESS'];
$city=$row_rsCity['CITY'];
$state=$row_rsCity['STATE'];
$phone=$row_rsCity['PHONENUMBER'];
$type=$row_rsCity['DEALER_TYPE'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc<br/>$city , $state<br />Phone: $phone',$type);\n");
} while ($row_rsCity = mysql_fetch_assoc($rsCity));
?>
center = bounds.getCenter();
map.fitBounds(bounds);
}
I'm close but I can't find similar examples online so looking for a little help in solving this issue.
Thanks!
When you call the function addMarker, you need to pass the type of icon (numeric) via the type in your function list.
Then when you are adding the marker. :
function addMarker(lat, lng, info, type) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: iconType[type],
map: map
});
Add in the numeric type to reference the correct icon in your icon array.......
I haven't ever done it this way before but I don't see why it wouldn't work.
If that doesn't work you may want to look at this example. :
Change individual markers in google maps directions api V3
Google Maps API v3: How do I dynamically change the marker icon?