Google Maps V3 Label alignment - google-maps

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>

Related

Custom icon image on google map polyline

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>

Image generator in Googlemaps

I wonder how I can generate an image, actually a small circle
with color and number as parameter in Googlemaps?
So for example
MakeImage($FF0000, 5)
Will draw a red circle with number 5 in centre.
What is the best approach without pregenerate all possible
combinations as image-files?
In order to achieve this you can create an icon as Symbol interface and combine it with MarkerLabel. Note the presense of the property labelOrigin in the Symbol interface, it defines where you will put the label.
To demonstrate this approach I used the built-in SVG path google.maps.SymbolPath.CIRCLE. Have a look at the following example and run it to see circle Marker with number.
function initMap() {
var myLatLng = {lat: 47.363362, lng: 8.485823};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.SATELLITE
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!',
icon: {
fillColor: "#FF0000",
strokeColor: "#FF0000",
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
labelOrigin: new google.maps.Point(0,0)
},
label: {
text: "5",
color: "white",
fontWeight: "bold",
fontSize: "16px"
}
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap">
</script>
I hope this helps!
I suggest you to use custom markers, here and here you can find a well documented API and it's explained how to make markers with bitmaps and svg graphics. I suggest to use SVG path notation like this:
var map;
var my_location = { lat: 12.97, lng: 77.59 };
icon = {
//this is a string that define a circle path
path: "M 100, 100 m -75, 0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0",
//this is a string that defines a hex color
fillColor: '#FF0000',
//this is a float that defines the opacity value from 0.0 to 1
fillOpacity: .6,
//this is a couple that defines a center point for the SVG
anchor: new google.maps.Point(0,0),
//this is a integer that defines the scale factor
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: new google.maps.LatLng(-33.91722, 151.23064),
mapTypeId: 'roadmap'
});
function makeMarkerWithImageCircle(text,location){
var iconUrl = 'https://your_circl_image_url.png';
var marker = new google.maps.Marker({
position: location,
label: text,
icon: iconUrl,
map: map
});
}
function makeMarkerWithSVGCircle(text,location){
var marker = new google.maps.Marker({
position: location,
label: text,
draggable: false,
icon: icon,
map: map
});
}
//method to generate marker with custom image url and text
makeMarkerWithImageCircle("text",my_location);
//method to generate marker with custom svg and text
makeMarkerWithSVGCircle("text",my_location);
I guess you have your initMap() method wherein you initialise a Map instance
Then you can make your custom function to instantiate a custom Marker inside the Map map with a SVG as your icon property.
I didn't run this script, just wrote to explain how you can do this.
Have a nice day and I hope this was helpful (:

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>

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>

Google Maps: How to change Marker default red color and add label

I think this should be rather easy at first. However It takes me many hours searching on the Internet without getting a solution.
I can add a Marker on a Google Map with Label, as below:
// Adding a marker to the map
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(22.48, 114.20),
map: map,
title: 'My House',
label: {
text: 'A',
color: '#79E149'
}
});
The default color is red.
My question:
How can I change the Red color of Marker to other color, for example, Green?
I noticed that most answers suggest to use an external icon, for example:
// Adding a marker to the map
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(22.48, 114.20),
map: map,
title: 'My House',
**icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',**
label: {
text: 'A',
color: '#79E149'
}
});
However:
1. The label will not appear when use this kind of icons.
Actually I don't want to use external image as the URL may be changed later. In fact many icons seems disappear already, for example:
http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png
Thanks and best regards
Alex
Your label text is green (#79E149) so it blends in with the marker
You probably don't want a "dot" on the marker if you want to put a label on it (change the marker icon's name to "green" from "green-dot").
The LabelOrigin of the icon is wrong for that particular marker/label combination
proof of concept fiddle
code snippet:
var geocoder;
var map;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(22.48, 114.20),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Adding a marker to the map
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(22.48, 114.20),
map: map,
title: 'My House',
icon: {
url: 'http://maps.google.com/mapfiles/ms/icons/green.png',
labelOrigin: new google.maps.Point(15, 10)
},
label: {
text: 'A',
color: 'black'
}
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(22.49194, 114.204426),
map: map,
title: 'My Other House',
icon: {
url: 'http://maps.google.com/mapfiles/ms/icons/orange.png',
labelOrigin: new google.maps.Point(15, 10)
},
label: {
text: 'B',
color: 'black'
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
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>
Try this
GoogleMap googlemap;
googlemap.addMarker(new MarkerOptions()
.position(new LatLng( 22.48, 114.20))
.title("My House").snippet("A").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));