I am drawing custom markers on a map and adding labels based on how many people are at that particular address. If there are more than 5 I want to write the label 5+ instead of writing out 23 for example. 1 - 4 work just fine, but I cannot seem to pass the + in a manner that it will display. I do not want to use a static image/marker with the number if I can help it. Anyone done this before?
I have tried this:
var res_label = '5+'
var marker = new google.maps.Marker({
position: latLng,
map: map,
icon: img,
label: encodeURIComponent(res_label),
animation: google.maps.Animation.DROP
});
The pin drops but is blank. I tried wrapping the marker var and that didn't work either.
In a URL a + will be used as space, you must encode it: %2B
You should use encodeURIComponent() to encode the entire string(there may be more chars which must be encoded)
window.onload = function() {
document.body.firstChild.data = encodeURIComponent(document.body.firstChild.data);}
^1234567890ß´`?=)(/&%$§"!+*~ÄÖLK_:;-.,Q>%
sample-implementation:
function initialize() {
var goo = google.maps,
map = new goo.Map(document.getElementById('map_canvas'), {
zoom: 14,
center: {
lat: 0,
lng: 0
}
}),
//note:the dynamic chart-icons are deprecated and may be disabled som day
url = 'https://chart.googleapis.com/chart?' +
'chst=d_bubble_text_small&chld=',
marker = new goo
.Marker({
map: map,
position: map.getCenter(),
icon: url +
encodeURIComponent('bb|text|FFFFFF|000000')
}),
btn = document.createElement('input');
btn.setAttribute('type', 'button');
btn.setAttribute('value', 'push me!');
map.controls[goo.ControlPosition.TOP_CENTER].push(btn);
goo.event.addDomListener(btn, 'click', function() {
var txt;
if (txt = prompt('type a string', 'text')) {
marker
.setIcon(url + encodeURIComponent('bb|' + txt + '|FFFFFF|000000'))
}
})
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
height: 100%;
margin: 0;
padding: 0
}
input {
background: tomato;
font-size: 22px;
border-color: tomato;
border-radius: 8px;
padding: 4px;
}
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<div id="map_canvas"></div>
Related
Is there a way to achieve something exactly like this?
I need to add two separate locations on a single map. Two divs on the top-left are important.
I created a map and added two locations. But the view we get is completely different from the normal google maps.
You can use Google Maps API to do that as shown here (I've just apply some small modifications to original answer to make it pure JavaScript-based):
var mapDiv = document.createElement('div');
mapDiv.setAttribute("id", "map");
mapDiv.style.cssText = 'display: table; width: 100vw; height: 100vh;';
document.body.appendChild(mapDiv);
var locations = [
['<h3>Dnipro</h3>You must visit this city!', 48.47672904338519, 34.99977516526972, 4],
['<h3>Lvyv</h3>A western capital of Ukraine', 49.84196798934066, 24.0256235253287, 3],
['<h3>Kharkiv</h3>A first capital of Ukraine', 49.9968725638038, 36.232661130479805, 2],
['<h3>Kyiv</h3>A capital of Ukraine', 50.464300,30.494800, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: new google.maps.LatLng(50.4,30.4),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
You can see the the result on Codepen
Info divs can be added in that way (just a sample):
<style>
#over_map { position: absolute; top: 10px; left: 10px; z-index: 99;
width:200px;height:100px;border:1px solid black; background: white; }
</style>
<div id="over_map">
<h4>This is a first place</h4>
</div>
I have a map with a number of markers on it. Because of the way it is set up, one marker will always coincide with the centre of the map. I want to show a different icon on that marker to all the others.
The markers and their corresponding info box are defined with this function:
function createMarker(latlng, html, summary, photo1, photo2, thisLatlng) {
var contentString = "<div style='min-height:150px'>" + photo1 + "<img src='/images/" + photo2 + "' width='225' height='150' align='left' style='margin-right:8px;border:none'></a><span class='title'>" + html +"</span><br>" + summary + "</div>";
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: thisicon,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'mouseover', function() {
ib.close(map, marker);
boxText.innerHTML = contentString;
ib.open(map, marker);
});
google.maps.event.addListener(map, 'click', function() {
ib.close(map, marker);
});
gmarkers.push(marker);
}
"latlng" is the a lat/long that is different for each marker. "thisLatlng" is the centre of the map and the lat/long of one specific marker.
My intention was to use something like this to determine the correct icon to use:
if(latlng===thisLatlng){
var thisicon = icon1;
}else{
var thisicon = icon2;
}
However it's not working as the "if" always returns false. I've tried
if(latlng===thisLatlng){
and
if(latlng==thisLatlng){
and I've also tried turning it the other way around with != and !== but they always return false.
I've checked that the two values are equal by printing them one above the other and they look exactly the same (as they should be).
I must be doing something wrong, but what?
The LatLng class has an equals method.
equals
equals(other)
Parameters:
other: LatLng
Return Value: boolean
Comparison function.
The other option is to compute the difference between the two points and set a threshold (say 1 meter) for "equals"
That said, your existing code (with === works for me): fiddle as does .equals (fiddle)
code snippet:
var map;
var gmarkers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 19.12170,
lng: 72.85083
},
zoom: 13.5,
mapTypeId: 'terrain'
});
createMarker(map.getCenter(), "center", "summary", "", "", map.getCenter());
var otherPt = new google.maps.LatLng(19.12, 72.851);
createMarker(otherPt, "not center", "summary2", "", "", map.getCenter());
}
function createMarker(latlng, html, summary, photo1, photo2, thisLatlng) {
var contentString = "<div style='min-height:150px'>" + photo1 + "<img src='/images/" + photo2 + "' width='225' height='150' align='left' style='margin-right:8px;border:none'></a><span class='title'>" + html + "</span><br>" + summary + "</div>";
if (latlng.equals(thisLatlng)) {
var thisicon = "http://maps.google.com/mapfiles/ms/micons/blue.png";
} else {
var thisicon = "http://maps.google.com/mapfiles/ms/micons/green.png";
}
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: thisicon,
});
}
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>
I am trying to set google map view according to my needs but unable to do. Actually I want to show google map like this
But it is showing like this
I am using this code:
function init_map() {
var var_location = new google.maps.LatLng(33.6934473,35.644258);
var var_mapoptions = {
center: var_location,
zoom: 12
};
var var_marker = new google.maps.Marker({
position: var_location,
map: var_map,
title:"Venice"});
var var_map = new google.maps.Map(document.getElementById("map-container"),
var_mapoptions);
var_marker.setMap(var_map);
}
google.maps.event.addDomListener(window, 'load', init_map);
any help will be appreciated.
Thanks
Set the center of the map to a different location than the marker.
function init_map() {
var var_location = new google.maps.LatLng(33.6934473, 35.644258);
var var_mapoptions = {
center: new google.maps.LatLng(33.691578, 35.523065),
zoom: 12
};
var var_marker = new google.maps.Marker({
position: var_location,
map: var_map,
title: "Venice"
});
var var_map = new google.maps.Map(document.getElementById("map-container"),
var_mapoptions);
var_marker.setMap(var_map);
}
google.maps.event.addDomListener(window, 'load', init_map);
html,
body,
#map-container {
height: 200px;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="center"></div>
<div id="map-container"></div>
In my application I want to group items addresses by areas, my items already use Google maps API to choose address and has Lat/Long coordinates. I want to group them by areas automatically.
The example would be
If we have address https://www.google.com/maps/place/Erfurto+g.+1,+Vilnius+04220,+Lietuva/#54.6765968,25.2102183,15.5z/data=!4m2!3m1!1s0x46dd9398506f84bd:0x6cc62f1d26bc6613
It should automatically be assigned to area, marked here:
https://www.google.com/maps/place/Lazdynai,+Vilnius,+Lietuva/#54.6702541,25.1870655,14z/data=!4m2!3m1!1s0x46dd93a6cc53ba05:0x2600d18d4c454331
Areas should be also administered in my application.
As I understand I should store all MultiPolygon coordinates in my back-end, and then use some algorithm to find if the coordinates of address belong to that polygon? Am I right? Or I can fetch that somehow using Google Map API?
I made some very rough polygons (sorry if I insult anybody from Vilnius :) ), but I think you'll get the idea.
the main method you're looking for is google.maps.geometry.poly.containsLocation()
So I think this gives you the components you need; the plinciles. But if there is something specific you want to happen, please tell me.
<!DOCTYPE html>
<html>
<head>
<style>
html, body, #map-canvas {
height: 400px;
margin: 0px;
padding: 0px
}
#my_div {
border: 1px solid grey;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<script>
// these are very rough estimations of the Elderships of Vilnius
// please use the correct boundaries
var polygonData = [
{name: 'Senamiestis', color: '#ff0000', points: [[54.68256489008578, 25.315074920654297],[54.67631238544733, 25.30803680419922],[54.670555259971174, 25.288639068603516],[54.6752205795425, 25.269927978515625],[54.68812186362444, 25.259113311767578],[54.69506701073871, 25.26529312133789],[54.68832031289468, 25.30099868774414]]},
{name: 'Rasos', color: '#00ff00', points: [[54.669066215366605, 25.305633544921875],[54.68554193480844, 25.329322814941406],[54.68335879002739, 25.362625122070312],[54.656754688760536, 25.345458984375],[54.610254981579146, 25.328292846679688],[54.60568162741719, 25.308380126953125],[54.65516583289068, 25.29705047607422]]},
{name: 'Antakalnis', color: '#0000ff', points: [[54.72699938009521, 25.30426025390625],[54.71589532099472, 25.34820556640625],[54.80780860259057, 25.500640869140625],[54.81967870427071, 25.335845947265625],[54.771385204918595, 25.300140380859375]]}
];
var polygons = [];
var map;
// random markers.
var markerData = [
[54.75478050308602,25.3638149499893],
[54.68324427673198,25.27517330646513],
[54.70583916710748,25.240154385566694],
[54.68453433466152,25.293562531471235],
[54.72900384013322,25.330534100532514],
[54.682078227560325,25.28394949436186],
[54.65034635955749,25.30793917179106]
];
var markers = [];
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(54.682611637187925,25.287838697433454), // Vilnius university
mapTypeId: google.maps.MapTypeId.ROADMAP
};
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// draw the markers
var markerLocations = toLatLng(markerData);
for(var i in markerLocations) {
markers.push( new google.maps.Marker({
position: markerLocations[i],
title: i, // I'll just set the index as title.
map: map
}) );
}
// draw the polygons
for(var j in polygonData) {
var points = toLatLng(polygonData[j].points);
polygons[j] = drawPolygon(points, polygonData[j].color, polygonData[j].name);
// let's see if markers are in this polygon
var content = '';
for(var i in markerLocations) {
if (google.maps.geometry.poly.containsLocation(markers[i].position, polygons[j])) {
// display
content += '<li>' + markers[i].title + '</li>';
// I guess what you really want to do, is put this data in an array, or so
}
}
document.getElementById('display-data').innerHTML += '<h3>' + polygonData[j].name + '</h3>' + '<ul>' + content + '</ul><hr/>';
}
}
// takes an array of coordinats, [ [], [], [] ] , and returns Google Maps LatLng objects
function toLatLng(point_arrays) {
var locations = [];
for(var i in point_arrays) {
locations.push( new google.maps.LatLng(point_arrays[i][0], point_arrays[i][1]));
}
return locations;
}
// draws a polygon
function drawPolygon(points, color, name) {
if(points.length < 3) {
return;
}
// #see https://developers.google.com/maps/documentation/javascript/examples/polygon-simple
polygon = new google.maps.Polygon({
paths: points,
strokeColor: color,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: color,
fillOpacity: 0.35,
title: name,
map: map
});
return polygon;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
ul, li {
list-style: none;
}
li {
display: inline;
border: 1px solid grey;
padding: 5px;
margin: 5px;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<div id="display-data"></div>
</body>
</html>
What you could do, is update the markers table in the database; add a field 'eldership'.
You send the data you get from my script to the server, with Ajax, you update the table (= you fill in the id of the eldership in the eldership field of the marker), so you only have to do this once (and you need to update the new markers ...).
Some troubles with the following code.
Works perfectly but I would display the assiociate legend in KML files to the makers when "click" on it...
I would like a general command, cause markers can be more than 500...
Any help ?
the URL : http://tequila357.wix.com/test-projet#!map/c161y
Thanks
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
// lets define some vars to make things easier later
var kml = {
a: {
name: "MAP BELGIUM",
url: "https://maps.google.be/maps/ms?authuser=0&vps=2&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d24e3bd5bbd990f5d"
},
b: {
name: "GARDIENNES",
url: "https://maps.google.be/maps/ms?authuser=0&vps=3&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d21a429f0f00d5400"
},
c: {
name: "CRECHES",
url: "https://maps.google.be/maps/ms?authuser=0&vps=2&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d22900ca6b246c6a1"
},
d: {
name: "CRECHES PRIV",
url: "https://maps.google.be/maps/ms?authuser=0&vps=2&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d24cf21e2a254a10c"
},
e: {
name: "MAGASINS STORES",
url: "https://maps.google.be/maps/ms?authuser=0&vps=2&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d24db929a46bae78c"
},
f: {
name: "BABYSITTERS",
url: "https://maps.google.be/maps/ms?authuser=0&vps=2&hl=fr&ie=UTF8&msa=0&output=kml&msid=208899208399411894595.0004d24dba905263481df"
},
// keep adding more if ye like
};
// initialize our goo
function initializeMap() {
var options = {
center: new google.maps.LatLng(50.5812, 4.5687),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), options);
createTogglers();
};
google.maps.event.addDomListener(window, 'load', initializeMap);
// the important function... kml[id].xxxxx refers back to the top
function toggleKML(checked, id) {
if (checked) {
var layer = new google.maps.KmlLayer(kml[id].url, {
preserveViewport: true,
suppressInfoWindows: true
});
// store kml as obj
kml[id].obj = layer;
kml[id].obj.setMap(map);
}
else {
kml[id].obj.setMap(null);
delete kml[id].obj;
}
};
// create the controls dynamically because it's easier, really
function createTogglers() {
var html = "<form><ul>";
for (var prop in kml) {
html += "<li id=\"selector-" + prop + "\"><input type='checkbox' id='" + prop + "'" +
" onclick='highlight(this,\"selector-" + prop + "\"); toggleKML(this.checked, this.id)' \/>" +
kml[prop].name + "<\/li>";
}
html += "<li class='control'><a href='#' onclick='removeAll();return false;'>" +
"Remove all layers<\/a><\/li>" +
"<\/ul><\/form>";
document.getElementById("toggle_box").innerHTML = html;
};
// easy way to remove all objects
function removeAll() {
for (var prop in kml) {
if (kml[prop].obj) {
kml[prop].obj.setMap(null);
delete kml[prop].obj;
}
}
};
// Append Class on Select
function highlight(box, listitem) {
var selected = 'selected';
var normal = 'normal';
document.getElementById(listitem).className = (box.checked ? selected: normal);
};
function startup() {
// for example, this toggles kml b on load and updates the menu selector
var checkit = document.getElementById('a');
checkit.checked = true;
toggleKML(checkit, 'a');
highlight(checkit, 'selector1');
}
// test legen
google.maps.event.addListener(map, 'click', function(event) {
marker = new google.maps.Marker({position: event.latLng, map: map});
});
// end legend
</script>
<style type="text/css">
.selected { font-weight: bold; }
</style>
</head>
<body onload="startup()">
<div id="map_canvas" style="width: 100%; height: 700px;"></div>
<div id="toggle_box" style="position: absolute; top: 100px; right: 20px; padding: 10px; background: #fff; z-index: 5; "></div>
</body>
</html>
If you are looking for a "sidebar", that is not currently possible with KmlLayer.
You have at least two options:
import the KML into FusionTables, use FusionTablesLayer to display the markers/polygon on the map, query the table in an onclick handler for the sidebar to open the InfoWindow.
example
Use a third party KML parser like geoxml3 or geoxml-v3
example using geoxml3
To enable the InfoWindow to appear on click, remove this from the the code that initializes the KmlLayer:
suppressInfoWindows: true