Removing Markers in Google Maps API v3 JS - google-maps

I have this code in javascript and html, everything works perfectly but i tried many way to remove markers in google maps. Help me, thank you !
<script type="text/javascript">
function initialize() {
var markers = [
{
"title": 'ABC',
"lat": '19.1759668',
"lng": '72.79504659999998',
"description": 'ZXVZXV'
},
{
"title": 'ASDASD',
"lat": '19.0883595',
"lng": '72.82652380000002',
"description": 'ZXVZXA.'
},
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 25,
center: new google.maps.LatLng(19.1759668, 72.79504659999998),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true,
mapTypeId: 'satellite'
});
for (var i = 0; i < markers.length; i++) {
var data = markers[i];
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title,
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);

You could use top window array of markersObject eg markersObj store the markers object inside and then looping for set null or set map
<script type="text/javascript">
var markersObj = [];
var map;
function initialize() {
var markers = [
{
"title": 'ABC',
"lat": '19.1759668',
"lng": '72.79504659999998',
"description": 'ZXVZXV'
},
{
"title": 'ASDASD',
"lat": '19.0883595',
"lng": '72.82652380000002',
"description": 'ZXVZXA.'
},
];
map = new google.maps.Map(document.getElementById('map'), {
zoom: 25,
center: new google.maps.LatLng(19.1759668, 72.79504659999998),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true,
mapTypeId: 'satellite'
});
for (var i = 0; i < markers.length; i++) {
var data = markers[i];
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title,
});
markersObj.push(marker);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
function hideMarkers() {
nElem = markersObj.length
for (i=0; i< nElem; i++){
markersObj[i].setMap(null);
}
}
function showMarkers() {
nElem = markersObj.length
for (i=0; i< nElem; i++){
markersObj[i].setMap(map);
}
}
....
You need two buttons that call hideMarkers or showMarkers respectively

Related

How to automatically remove markers depending on Boolean value in JSON file

I have a JSON file:
[
{
"id": 1,
"city": "Milan",
"availability": true,
"lat": 45.4655,
"long": 9.1865,
"marker": "example.png"
},
{
"id": 2,
"city": "Berlin",
"availability": true,
"lat": 52.520008,
"long": 13.404954,
"marker": "example.png"
},
{
"id": 3,
"city": "Paris",
"availability": false,
"lat": 48.864716,
"long": 2.349014
}
]
What happens so far is if a city has "availability": true then the "marker" within the JSON file appears on my map. However the other city's that have "availability": false are showing the Google Maps red marker on my map when I don't want any marker to show if the "availability": false.
This is my code so far:
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(52.520008, 13.404954),
disableDefaultUI: true,
zoom: 2
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
$.getJSON('example.com', function(data) {
$.each(data, function(i, value) {
var myLatlng = new google.maps.LatLng(value.lat, value.long);
var marker = new google.maps.Marker({
position: myLatlng,
icon: value.marker,
map: map
});
});
});
}
Any help on how to remove these Google Maps Red Markers on a city with "availability": false would be great. Thanks in advance.
Check for the value in the loop, also you don't need a variable assigned the marker, it get's lost in the loop anyways.
$.getJSON('example.com', function (data) {
$.each(data, function (i, value) {
// Don't create a marker if field is false
if (value.availability === false) {
return;
}
// No variables used
new google.maps.Marker({
position: new google.maps.LatLng(value.lat, value.long),
icon: value.marker,
map: map
});
});
});
const filtered = data.filter(item => item.availability);
$.each(filtered, function(i, value) {
var myLatlng = new google.maps.LatLng(value.lat, value.long);
var marker = new google.maps.Marker({
position: myLatlng,
icon: value.marker,
map: map
});
});

Google Maps Icon is not showing on website

I'm trying to learn myself some more advanced code writing. And this week I'm learning about google maps and how to customize it.
I think I'm doing fine, but I have a problem with making an icon marker show on the actual website.
The program I use to make the website is dreamweaver and in the live view option the customized map and icon are both showing correctly. However, when I go to the actual website, the icon is missing. There is no marker at all on the google map. I could really use some help with this one.
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var styles = [
{
stylers: [
{ hue: "#0D6C91" },
{ saturation: 0 }
]
},{
featureType: "road",
elementType: "geometry",
stylers: [
{ lightness: 100 },
{ visibility: "simplified" }
]
}
];
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(51.834250, 4.309755),
disableDefaultUI: true,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};
var map = new google.maps.Map(document.getElementById('column_links'),
mapOptions);
var styledMap = new google.maps.StyledMapType(styles,
{name: "Styled Map"});
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
var image = '3. afbeeldingen/RLogo.png';
var myLatlng = new google.maps.LatLng(51.834250, 4.309755);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon: image,
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

"dragend" method for marker in Google Maps

I have a problem with my marker in Google Maps. This is my code:
var map;
var geocoder;
var current;
var styles = [
{
stylers: [
{ hue: "#00c0eb" },
{ saturation: -10 }
]
},{
featureType: "road",
elementType: "geometry",
stylers: [
{ lightness: 100 },
{ visibility: "simplified" }
]
},{
featureType: "road",
elementType: "labels",
stylers: [
{ visibility: "on" }
]
}
];
var NewEventMarker;
function changeMarkerPosition(marker, ll) {
marker.setPosition(ll);
}
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
panControl: false,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.BOTTOM_CENTER
},
scaleControl: false,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
map.setOptions({styles: styles});
google.maps.event.addListener(map, 'click', addNewEvent);
}
function codeAddress() {
var address = "London";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var image = new google.maps.MarkerImage("/img/markers/user-m.png",
new google.maps.Size(32.0, 49.0),
new google.maps.Point(0, 0),
new google.maps.Point(16.0, 24.0)
);
var marker = new google.maps.Marker({
map: map,
icon: image,
flat: false,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function addNewEvent(event){
var NEMlatLng;
if(NewEventMarker == undefined){
NewEventMarker = new google.maps.Marker({
map: map,
draggable: true,
icon: "/img/markers/marker-add.png",
position: event.latLng,
title: "Добавить событие"
});
} else {
changeMarkerPosition(NewEventMarker, event.latLng);
}
google.maps.event.addListener(NewEventMarker,'dragend',function(event) {
alert(event.latLng);
});
}
The idea of this code is when user clicks on a map, start work function - addNewEvent, and as you can see, if there is already exists "NewEventMarker", it just moves this marker to a place where user clicks, and if there are no marker, it creates. So if you try my code you can see, that if i click, for example two or three times on a map, marker will moves, but when i drag my marker, starts work function that assigned to 'dragend' event. But it will be works so many times, how much i clicked on a map. How can i fix it?
When i try to add google.maps.event.addListener(NewEventMarker,'dragend'....... to other places in my code i get an error: Cannot read property '_e3' of undefined
Please help me ;)
Evgeny, you did almost everything right but for each click you attach new event handler for dragend. Because of that you got several dragend events and for each event one alert.
Solution is to just move event handler to if statement and add event listener only in case if NewEventMarker is created (only first time):
function addNewEvent(event){
var NEMlatLng;
if (NewEventMarker == undefined){
NewEventMarker = new google.maps.Marker({
map: map,
draggable: true,
icon: "marker-add.png",
position: event.latLng,
title: "Добавить событие"
});
google.maps.event.addListener(NewEventMarker, 'dragend', function(event) {
alert(event.latLng);
});
} else {
changeMarkerPosition(NewEventMarker, event.latLng);
}
}

Google Maps API v3 multiple clickable markers

I'm trying to put multiple markers on Google Maps but making them each refers to an URL. I put all the markers in a loop but only the last one is executed. I want all of them to work.
Here is my code:
<script type="text/javascript">
function initialize() {
var json = [
{
"title": "title1",
"lat": 46.077428,
"lng": 18.229837
},
{
"title": "title2",
"lat": 46.042229,
"lng": 18.227134
},
{
"title": "title3",
"lat": 46.082831,
"lng": 18.225911
},
{
"title": "title4",
"lat": 46.092058,
"lng": 18.185645
},
{
"title": "title5",
"lat": 46.075493,
"lng": 18.22885,
"description": "some description to the 5th element",
"url": "http://www.pannon-home.hu/"
},
{
"title": "title6",
"lat": 46.075344,
"lng": 18.227713,
"description": "some description to the 6th element",
"url": "http://www.pannon-home.hu/"
}
]
var latlng = new google.maps.LatLng(46.071325, 18.233185);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var firstmap = new google.maps.Map(document.getElementById("allmap"),myOptions);
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: firstmap,
title: data.title,
url: data.url,
icon: 'home.png'
});
}
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, "mouseover", function(e) {
infoWindow.setContent(data.description);
infoWindow.open(firstmap, marker);
});
google.maps.event.addListener(marker, "mouseout", function() {
infoWindow.setContent("");
infoWindow.close();
});
google.maps.event.addListener(marker, "click", function() {
location.assign(marker.url);
});
}
</script>
create a new marker object
and marker.setMap(map);
Too much of your logic lies outside of your for loop, causing much of your logic to only be applied to the last marker. Try this adjustment:
<script type="text/javascript">
function initialize() {
var json = [
{
"title":"title1",
"lat":46.077428,
"lng":18.229837
},
{
"title":"title2",
"lat":46.042229,
"lng":18.227134
},
{
"title":"title3",
"lat":46.082831,
"lng":18.225911
},
{
"title":"title4",
"lat":46.092058,
"lng":18.185645
},
{
"title":"title5",
"lat":46.075493,
"lng":18.22885,
"description":"some description to the 5th element",
"url":"http://www.pannon-home.hu/"
},
{
"title":"title6",
"lat":46.075344,
"lng":18.227713,
"description":"some description to the 6th element",
"url":"http://www.pannon-home.hu/"
}
]
var latlng = new google.maps.LatLng(46.071325, 18.233185);
var myOptions = {
zoom:12,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var firstmap = new google.maps.Map(document.getElementById("allmap"), myOptions);
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position:latLng,
map:firstmap,
title:data.title,
url:data.url,
icon:'home.png'
});
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, "mouseover", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(firstmap, marker);
});
google.maps.event.addListener(marker, "mouseout", function () {
infoWindow.setContent("");
infoWindow.close();
});
google.maps.event.addListener(marker, "click", function () {
location.assign(marker.url);
});
}
}
</script>

Why google map is not drawing a polylines?

I am making a Polyline on google maps. But i can't see it. Following is the code:
$('#mapdiv').html('<div id="map_convas" style="width:640px; height:427px; margin-left:auto; margin-right:auto"></div>');
var centerLatLng = new google.maps.LatLng(obj[i].centerlat, obj[i].centerlong);
var myOptions = {
center: centerLatLng,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
draggable: true
};
var map_polyline;
var map = new google.maps.Map(document.getElementById("map_convas"),
myOptions);
var marker = new google.maps.Marker({
clickable: true,
position: centerLatLng,
map: map,
zIndex: 1
});
marker.setIcon('http://localhost:8888/images/marker.png');
var locations = obj[0].locations;
// in my current case locations.length is 1
for(var p=0; p<locations.length; p++){
var triangleCoords = new Array();
var markerArray = locations[p];
for(var x=0;x<markerArray.length;x++){
triangleCoords.push(new google.maps.LatLng(markerArray[x].latitude, markerArray[x].longitude));
}
map_polyline = new google.maps.Polyline({
path: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
map_polyline.setMap(map);
}
// doing some stuff
google.maps.event.trigger(map, "resize");
Json which i am parsing and making obj is following:
[
{
"centerlat": 55.68369272317236,
"centerlong": 13.176000376097194,
"locations": [
[
{
"longitude": 13.17480473780369,
"latitude": 13.17480473780369
},
{
"longitude": 13.17456847989296,
"latitude": 13.17456847989296
},
{
"longitude": 13.17426541821395,
"latitude": 13.17426541821395
},
{
"longitude": 13.1741324253503,
"latitude": 13.1741324253503
},
{
"longitude": 13.17386236043011,
"latitude": 13.17386236043011
},
{
"longitude": 13.17352138460909,
"latitude": 13.17352138460909
}
]
]
}
]
and i am parsing it using following line of code:
var obj = jQuery.parseJSON(result);
Everything else like center of map, marker and size is working fine. The only thing is that i can not see any polyline on the map. i put some alert to check values of markerArray and triangleCoords. They are fine. Can anybody tell me why can't i see the polylines? I am using more maps intances before. Is there any problem because of them? Thanks in advance.