Google Maps add remove Polyline - google-maps

i've a question about 'removing Polylines' from Google Maps. In this document is explained, how can be added or removed a polyline. (https://developers.google.com/maps/documentation/javascript/examples/polyline-remove?hl=de)
But my situation is a little bit different.
I've a 'addPolyline' function, which adds polyline on Google Maps. With this function i can add Polylines, so i can return 'polyline Object', but i can't remove this polyine with same way.
If you click on 'addLines', it can be drawn a line, but if i click on removeLine, it will not be removed.
var map;
var flightPlanCoordinates;
function addPolyline(el){
polyName = new google.maps.Polyline({
path: el,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
return polyName;
}
function initialize() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
}
function addLine(el) {
el.setMap(map);
}
function removeLine(el) {
el.setMap(null);
}
google.maps.event.addDomListener(window, 'load', initialize);
Here is Fiddle : http://jsfiddle.net/aldimeola1122/gna00zwb/
How can i achieve that, can you help me?
Thanks in advance.

You are creating a new polyline, then removing it.
<input onclick="removeLine(addPolyline(flightPlanCoordinates));" type=button value="Remove line">
If you want to remove the existing polyline, you need to keep a reference to it.
<input onclick="removeLine(polyline);" type=button value="Remove line">
<input onclick="polyline=addLine(addPolyline(flightPlanCoordinates));" type=button value="Add line">
working fiddle
var map;
var flightPlanCoordinates;
var polyline;
function addPolyline(el) {
polyName = new google.maps.Polyline({
path: el,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
return polyName;
}
function initialize() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
}
function addLine(el) {
el.setMap(map);
return el;
}
function removeLine(el) {
el.setMap(null);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#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"></script>
<div id="panel">
<input onclick="removeLine(polyline);" type=button value="Remove line">
<input onclick="polyline=addLine(addPolyline(flightPlanCoordinates));" type=button value="Add line">
</div>
<div id="map-canvas"></div>

Related

How I can left align the google maps

How to left align the google maps, I mean the marker positions. Here is my JS
function showGoogleMaps() {
var latLng = new google.maps.LatLng(position[0], position[1]);
var mapOptions = {
zoom: 16,
zoomControl:false,
mapTypeControl:false,
streetViewControl: false, // hide the yellow Street View pegman
scaleControl: true, // allow users to zoom the Google Map
mapTypeId:ROADMAP,
center: latLng,
scrollwheel: false,
styles: styles
};
map = new google.maps.Map(document.getElementById('googlemaps'),
mapOptions);
// Show the default red marker at the location
marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: false,
title: 'Hello',
animation: google.maps.Animation.DROP
});
}
Here are the screenshots
The default marker location
How I want this to be
The marker or location is centered by default. I want that either to the left or right by default.
Thanks
A simple approach:
Initially set the width of the map to e.g 30%.
The center of the map now will be in the middle of these 30%.
Once the map is loaded set the width of the map to 100%.
function initialize() {
var goo = google.maps,
map = new goo.Map(document.getElementById('googleMap'), {}),
markers = [{
pos: new goo.LatLng(26.1445169, 91.7362)
}, {
pos: new goo.LatLng(26.2571285, 92.05991)
}, {
pos: new goo.LatLng(26.3143518, 91.0497609)
}, ],
bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; ++i) {
new google.maps.Marker({
map: map,
position: markers[i].pos
});
bounds.extend(markers[i].pos);
}
map.fitBounds(bounds);
goo.event.addListenerOnce(map, 'idle', function() {
this.getDiv().style.width = '100%';
goo.event.trigger(this, 'resize');
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_wrapper,
#googleMap {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#googleMap {
width: 30%;
}
<div id="map_wrapper">
<div id="googleMap"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
To align the markers on the right additionally use the panBy-method:
function initialize() {
var goo = google.maps,
map = new goo.Map(document.getElementById('googleMap'), {}),
markers = [{
pos: new goo.LatLng(26.1445169, 91.7362)
}, {
pos: new goo.LatLng(26.2571285, 92.05991)
}, {
pos: new goo.LatLng(26.3143518, 91.0497609)
}, ],
bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; ++i) {
new google.maps.Marker({
map: map,
position: markers[i].pos
});
bounds.extend(markers[i].pos);
}
map.fitBounds(bounds);
goo.event.addListenerOnce(map, 'idle', function() {
this.getDiv().style.width = '100%';
this.panBy(-(this.getDiv().offsetWidth/1.5),0);
goo.event.trigger(this, 'resize');
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_wrapper,
#googleMap {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#googleMap {
width: 30%;
}
<div id="map_wrapper">
<div id="googleMap"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>

Can't get markers to work on google maps

I've been trying for ages to get a marker on this map, same location as center, but can't get it to work.
<html>
<head>
<style>
#map {
width: 100%x;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(59.909968, 10.748012),
zoom: 16,
zoomControl: true,
scaleControl: true,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
I've tried following the instructions here, but I can't get it to work..
Any ideas? I had it working on my site using an iframe, but had issues scrolling past the full width map without getting caught zooming in, so I'm looking for a way to make this code to replace it.
Following the example you link to works for me (setting the marker to the map center):
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(59.909968, 10.748012),
zoom: 16,
zoomControl: true,
scaleControl: true,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
// from https://developers.google.com/maps/documentation/javascript/examples/marker-simple
var marker = new google.maps.Marker({
// change the position to the map center
position: map.getCenter(),
map: map,
title: 'Hello World!'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

in markerwithlabel, labelcontent not displaying

Here is my code for google map marker with label. I have included following javascript files:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script type="text/javascript" src="js/markerwithlabel.js"></script>
and code for map is:
function initialize() {
var latLng = {lat: 12.923157, lng: 80.153337};
var homelatLng = new google.maps.LatLng(12.923157, 80.153337);
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: latLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new MarkerWithLabel({
position: homelatLng,
map: map,
labelContent: "ihoser",
labelAnchor: new google.maps.Point(22, 0),
labelClass: "map_marker_label", // the CSS class for the label
labelStyle: {opacity: 0.75}
});
var infowindow = new google.maps.InfoWindow({
content: "content";
});
google.maps.event.addListener(marker, 'click', function(e) {
infowindow.open(map,this);
});
infowindow.open(map,marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
The style included is:
.map_marker_label {
color: red;
background-color: white;
font-family: "Lucida Grande", "Arial", sans-serif;
font-size: 10px;
font-weight: bold;
text-align: center;
width: 40px;
height:20px;
border: 2px solid black;
white-space: nowrap;}
The map appears with marketr, infowindow and label. But the label is empty. In fact, labelContent does not load into DOM itself and even after centering the label by providing point, it seems to start at (0, 0).
I am attaching the image of the map.
Make sure you are using the latest version of markerwithlabel.js.
And there is a typo at line:
var infowindow = new google.maps.InfoWindow({
content: "content";
});
Replace it to
var infowindow = new google.maps.InfoWindow({
content: "content"
});
Working example
function initialize() {
var latLng = { lat: 12.923157, lng: 80.153337 };
var homelatLng = new google.maps.LatLng(12.923157, 80.153337);
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: latLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new MarkerWithLabel({
position: homelatLng,
map: map,
labelContent: "ihoser",
labelAnchor: new google.maps.Point(22, 0),
labelClass: "map_marker_label", // the CSS class for the label
labelStyle: { opacity: 0.75 }
});
var infowindow = new google.maps.InfoWindow({
content: "content"
});
google.maps.event.addListener(marker, 'click', function (e) {
infowindow.open(map, this);
});
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map {
height: 480px;
width: 640px;
margin: 0px;
padding: 0px;
}
.map_marker_label {
color: red;
background-color: white;
font-family: "Lucida Grande", "Arial", sans-serif;
font-size: 10px;
font-weight: bold;
text-align: center;
width: 40px;
height:20px;
border: 2px solid black;
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"></div>
Plunker

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>

Clear Only Rectangle Drawing(Not Markers)

Can you please take a look at this Demo and let me know how I can clear ONLY the Rectangle (Not Markers) after zoom in finshed.
I already tried adding the map.setMap(null); into rectanglecomplete but it is not working besides I think this might remove all drawing even the markers from the map which I do not want to do
google.maps.event.addListener(drawingManager,'rectanglecomplete',function(r) {
map.fitBounds(r.getBounds());
map.setMap(null);
});
Update
I also tried this:
var drawings = [];
function deleteRect() {
for (var i=0; i < drawings.length; i++)
{
drawings[i].overlay.setMap(null);
}
drawings = [];
}
google.maps.event.addListener(drawingManager,'rectanglecomplete',function(r) {
drawings.push(r);
map.fitBounds(r.getBounds());
deleteRect()
});
but still no success and I am getting the
Uncaught TypeError: Cannot read property 'setMap' of undefined
in the console!
code snippet:
var map;
var drawingManager
$(document).ready(function () {
var latlng = new google.maps.LatLng(49.241943, -122.889318);
var myOptions = {
zoom: 12,
center: latlng,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.RECTANGLE]
}
});
google.maps.event.addListener(drawingManager,'rectanglecomplete',function(r) {
map.fitBounds(r.getBounds());
});
drawingManager.setMap(map);
});
body {
padding-top:25px;
}
#map_canvas {
width: 100%;
height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=drawing"></script>
<div class="container">
<div class="well">
<div id="map_canvas"></div>
</div>
</div>
You can draw a transparent rectangle this way
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.RECTANGLE]
},
rectangleOptions: {
fillColor: '#cccccc',
fillOpacity: 0,
strokeWeight: 5,
}
});
and for remove the rectangle use
google.maps.event.addListener(drawingManager,'rectanglecomplete',function(r) {
map.fitBounds(r.getBounds());
r.setMap(null);
});
like this sample