Google Maps: Dynamically Movable and Resizable Circle Overlay Script for v3 - google-maps

Ok, I just found the perfect script I was looking for.
http://web3o.blogspot.com.br/2010/05/google-maps-dynamically-movable-and.html
You can add circles with lat/long and radius on Google Maps.
The problem is that this script it's only for v2 api.
I found this one too
http://www.mapdevelopers.com/draw-circle-tool.php
But there's no code or source available on it :(
Does anybody know any script like these that could work on v3?
Thank you

That functionality is built in to the Google Maps API v3 Drawing Library
Example
You can customize it to just allow circles (or just allow one circle).
Working example
code snippet:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.CIRCLE,
drawingControl: false,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.CIRCLE
]
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 1,
strokeWeight: 5,
clickable: false,
editable: true,
zIndex: 1
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas,
#map_canvas {
height: 100%;
}
#media print {
html,
body {
height: auto;
}
#map-canvas,
#map_canvas {
height: 650px;
}
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>

function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.CIRCLE,
drawingControl: false,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.CIRCLE
]
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 1,
strokeWeight: 5,
clickable: false,
editable: true,
zIndex: 1
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas,
#map_canvas {
height: 100%;
}
#media print {
html,
body {
height: auto;
}
#map-canvas,
#map_canvas {
height: 650px;
}
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing"></script>
<div id="map-canvas"></div>

Related

How to use CSS-only for cluster icons for Google Maps

My goal is to have CSS-only icons for my Google Maps marker clusters. I have seen in the documentation's "advanced example" (link) that CSS-only icons are possible. However, I have been unable to translate this example into my own project.
I have attempted to build a JSFiddle with my code, though I can not seem to initialize the map through JSFiddle due to API restrictions. When I run the code on my site, it adds numbers to the map, but no icons, as seen below.
I created some styles
var styles = [{
width: 30,
height: 30,
className: "custom-clustericon-1",
},
{
width: 40,
height: 40,
className: "custom-clustericon-2",
},
{
width: 50,
height: 50,
className: "custom-clustericon-3",
},
];
And I have attempted to initialize like this:
var markerCluster = new MarkerClusterer(map, markers,
{
styles: styles,
clusterClass: "custom-clustericon",
});
Where am I missing the mark here? I'd like to have marker icons exactly like the ones in the "advanced example", but I'm at a loss. I have searched extensively online for examples of css-only icons but could not find any standalone examples. Your help is kindly appreciated.
You are using the wrong version of the MarkerClusterer library.
Use this one:
<script src="https://googlemaps.github.io/js-markerclustererplus/dist/index.min.js"></script>
proof of concept fiddle
code snippet:
function map_initialize() {
var mapoptions = {
center: new google.maps.LatLng(0, 0),
zoom: 0,
maxZoom: 15,
mapTypeId: google.maps.MapTypeId.TERRAIN,
disableDefaultUI: true,
zoomControl: true,
scrollwheel: true
};
var styles = [{
width: 30,
height: 30,
className: "custom-clustericon-1",
},
{
width: 40,
height: 40,
className: "custom-clustericon-2",
},
{
width: 50,
height: 50,
className: "custom-clustericon-3",
},
];
var map = new google.maps.Map(document.getElementById("mapdivbig"),
mapoptions);
var infoWin = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
var markers = locations.map(function(location, i) {
bounds.extend(location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
position: location
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
return marker;
});
var markerCluster = new MarkerClusterer(map, markers, {
styles: styles,
clusterClass: "custom-clustericon",
});
}
var locations = [{
lat: 45.4208,
lng: -123.8,
info: 'Location 1'
},
{
lat: 47.6117,
lng: -122.345,
info: 'Location 2'
},
{
lat: 47.6308,
lng: -122.375,
info: 'Location 3'
}
]
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#mapdivbig {
width: 100%;
height: 100%;
background-color: grey;
}
#mapdiv {
width: 100%;
height: 325px;
background-color: grey;
}
.custom-clustericon {
background: var(--cluster-color);
color: #fff;
border-radius: 100%;
font-weight: bold;
font-size: 15px;
display: flex;
align-items: center;
}
.custom-clustericon::before,
.custom-clustericon::after {
content: "";
display: block;
position: absolute;
width: 100%;
height: 100%;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
background: var(--cluster-color);
opacity: 0.2;
border-radius: 100%;
}
.custom-clustericon::before {
padding: 7px;
}
.custom-clustericon::after {
padding: 14px;
}
.custom-clustericon-1 {
--cluster-color: #00a2d3;
}
.custom-clustericon-2 {
--cluster-color: #ff9b00;
}
.custom-clustericon-3 {
--cluster-color: #ff6969;
}
<!DOCTYPE html>
<html>
<head>
<title>Marker Clustering</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://googlemaps.github.io/js-markerclustererplus/dist/index.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=map_initialize" defer></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="mapdivbig"></div>
</body>
</html>

how to add my location button to google map

i would like to add my location button to my map like the real google map does.
ihave tried some plugins but they didnt had location button, what should i do?
here is one plugin that i used:
var mapOptions = {
zoom: 17,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var GeoMarker = new GeolocationMarker(map);
For web browsers you can use the html5 built in geolocation to get your location: You can follow this doc: https://developers.google.com/maps/documentation/javascript/examples/map-geolocation
But first set a default location in case the browser doesn't support Geolocation
var myLocation = {lat: -34.397, lng: 150.644};
After that in your init function you can get your location like this:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLocation.lat = position.coords.latitude;
myLocation.lng = position.coords.longitude;
map.setCenter(myLocation);
}, function() {
handleLocationError(true, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, map.getCenter());
}
Set your location's coordinates as default center of the map
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: myLocation,
mapTypeId: 'roadmap'
});
Then just call this function if you want to go back to your location
function goToMyLocation() {
map.setCenter(myLocation);
}
Check this working example: https://jsbin.com/ricebu/edit?html,css,js,output
Here is the code snippet as well
var map;
var btnLocation = document.getElementById("btn-location");
var myLocation = {
lat: -34.397,
lng: 150.644
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: myLocation,
mapTypeId: 'roadmap'
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLocation.lat = position.coords.latitude;
myLocation.lng = position.coords.longitude;
// I just added a marker for you to verify your location
var marker = new google.maps.Marker({
position: myLocation,
map: map
});
map.setCenter(myLocation);
}, function() {
handleLocationError(true, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
console.log(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
btnLocation.addEventListener('click', function() {
goToMyLocation();
});
function goToMyLocation() {
map.setCenter(myLocation);
}
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#btn-location {
position: absolute;
right: 20px;
top: 20px;
z-index: 1;
padding: 20px;
border: none;
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.8);
transition: 0.5s;
}
#btn-location:hover {
background-color: rgba(0, 0, 0, 1);
color: white;
cursor: pointer;
}
<html>
<head>
<title>Location</title>
</head>
<body>
<button id="btn-location">Go to my Location</button>
<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=AIzaSyCKQX3cyZ7pVKmBwE8wiowivW9qH62AVk8&callback=initMap"></script>
</body>
</html>
var btnLocation = document.getElementById("btn-location");
var myLocation = {
lat: -34.397,
lng: 150.644
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: myLocation,
mapTypeId: 'roadmap'
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLocation.lat = position.coords.latitude;
myLocation.lng = position.coords.longitude;
// I just added a marker for you to verify your location
var marker = new google.maps.Marker({
position: myLocation,
map: map
});
map.setCenter(myLocation);
}, function() {
handleLocationError(true, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
console.log(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
btnLocation.addEventListener('click', function() {
goToMyLocation();
});
function goToMyLocation() {
map.setCenter(myLocation);
}
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#btn-location {
position: absolute;
right: 20px;
top: 20px;
z-index: 1;
padding: 20px;
border: none;
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.8);
transition: 0.5s;
}
#btn-location:hover {
background-color: rgba(0, 0, 0, 1);
color: white;
cursor: pointer;
}
<html>
<head>
<title>Location</title>
</head>
<body>
<button id="btn-location">Go to my Location</button>
<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=AIzaSyCKQX3cyZ7pVKmBwE8wiowivW9qH62AVk8&callback=initMap"></script>
</body>
</html>
You can also consider using https://www.npmjs.com/package/google-maps-current-location
It adds the typical button to the map, it handles the geolocation, and it also adds the blue circle surrounding the marker.
Hope it helps :)

Google Map marker text

I have found this icon: http://chart.apis.google.com/chart?cht=mm&chs=60x102&chco=ffffff,70a3c1,70a3c1&ext=.png
I'm wondering if it's possible to place text on this icon. Basically what I'm looking for is an icon with a dynamic color and text. A simple PHP script that dynamically generates an icon based on a images and text would also be great.
You can create image from png (using PHP GD )
$yourimage = imagecreatefrompng($originalImage);
Add in your html page this tag
<img src="test_icon.php?text=yourtext">
and the add the text you need
this way using test_icon.php
test_icon.php
<?php
header("Content-type: image/png");
$yourOrigImage = "group_icon.png";
if(file_exists($yourOrigImage)) {
$newImage = imagecreatefrompng($yourOrigImage);
imagesavealpha($newImage, true); // this for keep the png's transparency important
if(!$newImage) {
die("im is null");
}
$black = imagecolorallocate($newImage, 0, 0, 0);
$width = 36; // the width of the image
$height = 36; // the height of the image
$font = 4; // font size
$text = $_GET['text']; // text
$leftTextPos = 4;
$outputImage = "group_icon_".$text.".png";
imagestring($newImage, $font, $leftTextPos, 9, $text, $black);
imagepng($newImage, $outputImage, 0);
imagedestroy($newImage);
}
?>
Two options:
The third party MarkerWithLabel library.
fiddle
code snippet:
var map;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new MarkerWithLabel({
position: map.getCenter(),
map: map,
icon: "http://chart.apis.google.com/chart?cht=mm&chs=60x102&chco=ffffff,70a3c1,70a3c1&ext=.png",
labelContent: "A",
labelAnchor: new google.maps.Point(15, 90),
labelClass: "labels", // the CSS class for the label
labelInBackground: false
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
.labels {
color: black;
font-family: "Lucida Grande", "Arial", sans-serif;
font-size: 30px;
text-align: center;
width: 30px;
white-space: nowrap;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>
<div id="map_canvas"></div>
Add a label to the native google.maps.Marker
fiddle
code snippet::
var map;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
icon: "http://chart.apis.google.com/chart?cht=mm&chs=60x102&chco=ffffff,70a3c1,70a3c1&ext=.png",
label: "A"
});
}
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>

Where to add the Weather Layer on this custom map?

I need some help in figuring out where to put any of the weather layer code to a custom Google Map (API v3), any input would be greatly appreciated :)
We have created a custom Google Map, and on it we have 2 Fusion Table layers with a simple toggle. From the developer documentation, and what I have found here on this site, I can see I need to add this bit of code:
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.FAHRENHEIT
});
weatherLayer.setMap(map);
but I am totally clueless as to where to place this, as each time I have tried adding it (2 days now) the code breaks and I am shown a blank white page with all kinds of errors in the F12 Console window.
Where do we need to add that, and how can we add it to the toggle? Here is what we have working:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=weather"></script>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
var map;
var layer1;
var tableid = '';
var layer2;
var tableid2 = 'xxxxx';
var layer3;
var tableid3 = 'xxxxx';
function initialize() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(31.499, -111.202),
zoom: 10,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_CENTER
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: true,
scaleControlOptions: {
position: google.maps.ControlPosition.BOTTOM_LEFT
},
streetViewControl: false,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_TOP
}
});
var styledMapType = new google.maps.StyledMapType({
map: map,
name: 'Styled Map'
});
map.mapTypes.set('map-style', styledMapType);
map.setMapTypeId('map-style');
layer1 = new google.maps.FusionTablesLayer({
query: {
select: '',
from: tableid
},
map: map
});
layer2 = new google.maps.FusionTablesLayer({
query: {
select: 'LOC',
from: tableid2
},
map: map
});
layer3 = new google.maps.FusionTablesLayer({
query: {
select: 'geometry',
from: tableid3
},
suppressInfoWindows:true,
map: map
});
}
function changeMap(layerNum) {
if (layerNum == 2) {
update(layer2);
}
if (layerNum == 3) {
update(layer3);
}
}
function update(layer) {
var layerMap = layer.getMap();
if (layerMap) {
layer.setMap(null);
} else {
layer.setMap(map);
}
}
</script>
<style type="text/css">
#toggle_box {
position: absolute;
top: 7px;
right: 7px;
padding: 3px;
border: 1px solid #707735;
background: #DED9C6;
font-family: 'Coda', cursive;
}
body {
margin: 0px;
padding: 0px;
font-family: 'Coda', cursive;
}
#map_canvas {
position: absolute;
right: 0;
top: 0;
width: 100%;
height: 100%;
font-family: 'Coda', cursive;
}
</style>
</head>
<body onload="initialize();">
<div id="map_canvas"></div>
<div id="toggle_box"><input type="checkbox" value="2" onclick="changeMap(this.value)" checked="checked" />REFERENCE POINTS
<input type="checkbox" value="3" onclick="changeMap(this.value)" checked="checked" />AZ COUNTIES</div>
</body>
</html>
Place it somewhere where the map is initialized, e.g. before that line:
var styledMapType = new google.maps.StyledMapType({

Google maps InfoBox (infowindow) on top of the map (z-index), Yelp.com tooltip effect

This is a google map with an InfoBox, I want to achieve the Yelp's map tooltip div effect. In other words I want to position an infoBox on top of a google map, in order to do that I'm using a google maps InfoBox with a z-index higher than the map z-index, but it is not working, this is the jsfiddle help please,
http://jsfiddle.net/MdhzR/
In this example I want the yellow container to be on top of the map container and also on top of the gray container, is that possible?. help please!
Here is the code:
CSS:
#map_canvas{
position: absolute;
top: 50px; left: 20px;
width: 300px;
height: 300px;
z-index: 1;}
#z_index_gray {
position: absolute;
top: 250px; left: 250px;
height: 125px; width: 125px;
background-color: gray;
z-index: 10;}
HTML:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<div id="map_canvas"></div>
<div id="z_index_gray"></div>
<p>
JS:
function initialize() {
var secheltLoc = new google.maps.LatLng(49.47216, -123.76307);
var myMapOptions = {
zoom: 15
,center: secheltLoc
,mapTypeId: google.maps.MapTypeId.ROADMAP
};
var theMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
var marker = new google.maps.Marker({
map: theMap,
draggable: true,
position: new google.maps.LatLng(49.47216, -123.76307),
visible: true
});
var boxText = document.createElement("div");
boxText.style.cssText = "position: absolute;border: 1px solid black; background: yellow; padding: 5px;z-index: 20;height: 100px; width: 300px;";
boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";
var myOptions = {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: 1
,boxStyle: {
background: "url('tipbox.gif') no-repeat"
,opacity: 0.75
,width: "280px"
}
,closeBoxMargin: "10px 2px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(5, 5)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
google.maps.event.addListener(marker, "click", function (e) {
ib.open(theMap, this);
});
var ib = new InfoBox(myOptions);
ib.open(theMap, marker);
}
$(document).ready(function() {
initialize();
});
JSFiddle: http://jsfiddle.net/MdhzR/
Thanks!
PD. I'm following this infoBox documentation example: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html
I found this example from Pamela Fox and it does exactly what I want.
http://gmaps-samples-v3.googlecode.com/svn/trunk/smartinfowindow/smartinfowindow.html
I hope it helps to someone else.