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 (:
Related
I am wanting to put my own custom label over certain places on a map, take for example the image below where there is text for Albert Park Lake which is not associated with a place marker. This is an example of what I am wanting to do as I am designing an app that has a number of regions and I wanting to put the names of those regions on the map which will only be visible at certain zoom levels as to ensure there is no clutter when being viewed by the user. I had experimented with place markers but found that the text being on top of the marker was bad look and I would have had to write a function to adjust the text visibility for the zoom level to deal with the clutter of many being on the screen at once.
Cheers Jordan Smith
I suggest to convert the text you want to set to bitmap image and set this bitmap as custom marker, you may find this question useful if you want to know how to convert text to bitmap.
Here are a few things you could try:
1. Pop up multiple infowindows: https://jsfiddle.net/kjqfs0bo/
function initMap() {
const uluru = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: uluru,
});
const contentString ="lalala";
const infowindow = new google.maps.InfoWindow({
content: contentString,
});
const infowindow2 = new google.maps.InfoWindow({
content: contentString,
});
const marker = new google.maps.Marker({
position: uluru,
map,
title: "Uluru (Ayers Rock)",
});
const marker2 = new google.maps.Marker({
position: { lat: -20.363, lng: 130.044 },
map,
title: "Uluru (Ayers Rock)",
});
infowindow.open(map, marker);
infowindow2.open(map, marker2);
}
2. Invisible markers with text label: https://jsfiddle.net/0koynbz2/1/
function createMarker() {
var canvas, context;
canvas = document.createElement("canvas");
return canvas.toDataURL();
}
var map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: 13,
center: new google.maps.LatLng(56.1304, -106.3468),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(56.1304, -106.3468),
map: map,
icon: {
url: createMarker()
},
label: {
text: 'My Place',
color: '#000',
fontSize: '14px',
fontWeight: 'bold'
}
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(56.1204, -106.3468),
map: map,
icon: {
url: createMarker()
},
label: {
text: 'My Place2',
color: '#000',
fontSize: '14px',
fontWeight: 'bold'
}
});
3. Maybe try geojson, but since geojson only supports point, line and polygon. I'm not sure if there is a way to display text only.
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>
I am using google maps version 3 api and google maps marker. When the markers are close to each other the labels overlap and it looks bad. I was trying to give background color to the marker label but google maps marker label does not seem to have background color property. I tried using MarkerWithLabel api but rendering was slower when I had 1000s of markers compared to google maps deafult Marker Label. I have included the js fiddle. Can anyone please help me with this ?
https://jsfiddle.net/dcvc99qz/12/
function initMap() {
var pointA = new google.maps.LatLng(51.2750, 1.0870),
pointB = new google.maps.LatLng(51.2750, 0.8140),
myOptions = {
zoom: 5,
center: pointA,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
map: map
}),
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
map: map
});
}
initMap();
Markers are just images, you can't just change its background color, you have to define a new icon. First, prepare new transparent images with right colors. Then create new icon definitions.
var icons = {
green: {
// link to an image, use https if original site also uses https
url: 'https://i.imgur.com/5Yd25Ga.png',
// set size of an image
size: new google.maps.Size(22, 40),
// if an image is a sprite, set its relative position from 0, 0
origin: new google.maps.Point(0, 0),
// part of an image pointing on location on the map
anchor: new google.maps.Point(11, 40),
// position of text label on icon
labelOrigin: new google.maps.Point(11, 10)
},
red: {
// ...
}
};
Documentation of icon properties
When adding new marker set its icon to one of defined icons.
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
icon: icons.green,
map: map
});
When points are close to each other, you will notice that text labels are overlapping. To fix it, add increasing zIndex to every marker.
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
zIndex: 1000,
icon: icons.red,
map: map
});
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
zIndex: 1001,
icon: icons.green,
map: map
});
Full working example: jsfiddle.net/yLhbxnz6/
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)));
I want to have a google map on my website overlaid with rectangular shapes that can then be clicked onto to open a link. The rectangular shapes relate to buildings, so a larger building would be overlaid with a larger shape.
Clearly the shapes relate to an area of the map so would reduce in size as you zoomed out, and increase as you zoom in.
Is this possible with a google api, Can I do this ?
Yes, you can make clickable shapes with the GoogleMaps API,
You have to define the latitude and longitude points of the shape in an array, like the following:
var StackExchangeNYLatLon = [
{lat: 40.708788, lon: -74.006676},
{lat: 40.708808, lon: -74.006822},
{lat: 40.709009, lon: -74.006582},
{lat: 40.708963, lon: -74.006515}
];
Make a div element for the map to populate,
Then define the map (with the div id and options as parameters into new google.maps.Map):
map = new google.maps.Map(document.getElementById('your-div-id'), {
zoom: 5,
center: {lat: 40.709009, lon: -74.006582}, //This is the one of the provided above
mapTypeId: google.maps.MapTypeId.TERRAIN
});
Then, construct the polygon and set the map:
var StackExchangeNYHeadquarters = new google.maps.Polygon({
paths: StackExchangeNYLatLon,
strokeColor: 'blue',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: 'blue',
fillOpacity: 0.35
});
StackExchangeNYHeadquarters.setMap(map);
If you wanted a marker you could do the following code instead:
var marker = new google.maps.Marker({
position: options.latLonData,
map: map,
title: 'My House'
});
Now, add an eventListener to the polygon:
This is the part where you define what happens when you click the shape,
You could make it directly open the link, or open a nice little dialog box with content you can fill.
If you wanted to open a link you could have document.location.href, whereas if you wanted content, you'd need to append a contentString to the infoWindow.
StackExchangeNYHeadquarters.addListener('click', function(event){
document.location.href = 'http://example.com/mylink/';
});
and now, make the infoWindow:
infoWindow = new google.maps.InfoWindow;
All of the above code should be wrapped in a callback handler that is passed along as a query string in the API call:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=startMap"></script>
So the above code should look like:
function startMap(options){
var latLonData = options.latLonData;
var map = new google.maps.Map(document.getElementById('your-div-id'), {
zoom: 5,
center: options.center,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var polygon = new google.maps.Polygon({
paths: StackExchangeNYLatLon,
strokeColor: 'blue',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: 'blue',
fillOpacity: 0.35
});
polygon.setMap(map);
polygon.addListener('click', , function(event){
document.location.href = 'http://example.com/mylink/';
});
var infoWindow = new google.maps.InfoWindow;
}
function StackExchangeHeadquartersBuild(){
options = {
latlonData: [
{lat: 40.708788, lon: -74.006676},
{lat: 40.708808, lon: -74.006822},
{lat: 40.709009, lon: -74.006582},
{lat: 40.708963, lon: -74.006515}
],
center: {lat: 40.709009, lon: -74.006582}
};
startMap(options);
}
You could attach the callback to a factory, so that a lot of elements can be created at once without 500 individual functions for each location.