Get value of Select2 for Google Map Api v3 - google-maps-api-3

I recently changed my SELECT menu from the standard format to a Select 2 menu. Since this change my Value"" field seems to not be carrying the value to the map. When I select an item from the list it should trigger it's respective info box on the map to show, but his has stopped working. Any ideas?
Google Map Code Sample:
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(42.3443961,-71.0853170);
var settings = {
zoom: 14,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
<!--START Boston Medical Center-->
var contentStringboscen = '<div class="contentmap">'+
'<div id="siteNotice">'+
'</div>'+
'<h2 id="firstHeading" class="firstHeading">Boston Medical Center</h2>'+
'<div id="bodyContent">'+
'<p>Boston Medical Center (BMC) is a 496-bed academic medical center located in Boston’s historic South End. The hospital is the primary teaching affiliate for Boston University School of Medicine.</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentStringboscen
});
var boscenImage = new google.maps.MarkerImage('images/map-icons/local-attractions/a.png',
new google.maps.Size(40,50),
new google.maps.Point(0,0),
new google.maps.Point(50,50)
);
var boscenShadow = new google.maps.MarkerImage('images/map-icons/local-attractions/a-shadow.png',
new google.maps.Size(40,50),
new google.maps.Point(0,0),
new google.maps.Point(65, 50));
var boscenPos = new google.maps.LatLng(42.3344236,-71.0744651);
var boscenMarker = new google.maps.Marker({
position: boscenPos,
map: map,
icon: boscenImage,
shadow: boscenShadow,
title:"Boston Medical Center",
zIndex: 3});
<!--STOP Boston Medical Center-->
<!--START Boston Public Library-->
var contentStringboslib = '<div class="contentmap">'+
'<div id="siteNotice">'+
'</div>'+
'<h2 id="firstHeading" class="firstHeading">Boston Public Library</h2>'+
'<div id="bodyContent">'+
'<p>The Boston Public Library (est.1848) is a municipal public library system in Boston, Massachusetts, United States. It was the first publicly supported municipal library in the United States, the first large library open to the public in the United States, and the first public library to allow people to borrow books and other materials and take them home to read and use.</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentStringboslib
});
var boslibImage = new google.maps.MarkerImage('images/map-icons/local-attractions/b.png',
new google.maps.Size(40,50),
new google.maps.Point(0,0),
new google.maps.Point(50,50)
);
var boslibShadow = new google.maps.MarkerImage('images/map-icons/local-attractions/b-shadow.png',
new google.maps.Size(40,50),
new google.maps.Point(0,0),
new google.maps.Point(65, 50));
var boslibPos = new google.maps.LatLng(42.349484,-71.077740);
var boslibMarker = new google.maps.Marker({
position: boslibPos,
map: map,
icon: boslibImage,
shadow: boslibShadow,
title:"Boston Public Library",
zIndex: 3});
<!--STOP Boston Public Library-->
google.maps.event.addListener(boscenMarker, 'click', function() {
infowindow.setContent(contentStringboscen);
infowindow.open(map,boscenMarker);
});
google.maps.event.addListener(boslibMarker, 'click', function() {
infowindow.setContent(contentStringboslib);
infowindow.open(map,boslibMarker);
});
var selectChoices = {
boscenChoice: boscenMarker,
boslibChoice: boslibMarker,
};
google.maps.event.addDomListener(
document.getElementById("selectLocation"), 'change',
function() {
google.maps.event.trigger(selectChoices[this.value], "click");
});
}
</script>
Select2 Code:
<link type="text/css" rel="stylesheet" href="select2/select2.css">
<script src="select2/select2.js"></script>
<script>
var men=jQuery;
men.noConflict();
men(document).ready(function() {
men('#selectLocation').select2(
{
placeholder: "Choose an Attraction",
width:'400px',
formatResult: format,
formatSelection: format,
});
function format(option) {
if (option.id.indexOf('Choice') != -1) {
return "<span style='display:inline-block;width:20px'>" + option.text.substring(0,2) + "</span>" + option.text.substring(2)
} else {
return option.text
}
}
});
</script>
Select2 Menu:
<div class="general-bg" style="background-color:#ffffff; border-bottom:solid 1px #cccccc">
<div id="map-nav" style="width:400px;margin:10px auto 10px auto; background-color:#ffffff;">
<select id="selectLocation" style="color:#09F; font-size:18px; font-family:'Century Gothic W01';" name="categories">
<option></option>
<option value='boscenChoice'>A. Boston Medical Center</option>
<option value='boslibChoice'>B. Boston Public Library</option>
<option value='chrcenChoice'>C. Christian Science Center</option>
<option value='chuapaChoice'>D. Church Park Apartments</option>
<option value='copplaChoice'>E. Copley Place Shopping</option>
<option value='fenparChoice'>F. Fenway Park</option>
<option value='findisChoice'>G. Financial District</option>
<option value='houbluChoice'>H. House of Blues</option>
<option value='huntheChoice'>I. Huntington Theatre</option>
<option value='isamusChoice'>J. Isabella Stewart Gardener Museum</option>
<option value='logairChoice'>K. Logan International Airport</option>
<option value='lonmedChoice'>L. Longwood Medical and Academic Area</option>
<option value='mashosChoice'>M. Massachusetts General Hospital</option>
<option value='mastecChoice'>N. Massachusetts Institute of Technology</option>
<option value='musartChoice'>O. Museum of Fine Arts</option>
<option value='newstrChoice'>P. Newbury Street Shopping</option>
<option value='prucenChoice'>Q. Prudential Center Shopping</option>
<option value='pubgarChoice'>R. Public Garden</option>
<option value='symhalChoice'>S. Symphony Hall</option>
</select>
</div>
</div>

You problem come from Select2 behavior.
When you select a new entry inside Select2 the html attribute on the <option> inside your <select id="selectLocation"> will not be updated as "selected" like a normal select will do. Select2 will in exchange update <span class"select2-selection__rendered"> and trigger 2 event in javascript: change and select2:select
From there we learn that your code need to be updated here:
google.maps.event.addDomListener(
document.getElementById("selectLocation"), 'change',
function() {
google.maps.event.trigger(selectChoices[this.value], "click");
});
Those are the thing to fix
Where you attach the event document.getElementById("selectLocation") will not trigger. You need to attach it to a select2 event as mentioned above or directly on the <span class="select2-selection__rendered">
Where you get the value google.maps.event.trigger(selectChoices[this.value], "click"), since the <option> will not be updated you need to grab the value from the select2 event, select2 instance or directly on the <span class="select2-selection__rendered">
P.S. using the <span class="select2-selection__rendered"> will be a very fast fix to write for your code. Going for select2 event and select2 instance is a better approach but will be longer to apply and require more learning from your part.

Related

Google javascript map api works in VS 2017 but doesn't work when published to Azure

I have an ASP.NET Core 2.2 website that creates a map from an address using the google geocoding api and map javascript api. It works in Visual Studio 2017, but when I publish it to Azure, it doesn't work anymore. The map doesn't appear. The developer console shows no errors.
I use the Geocoding API to generate a latlong, from the street address. I added a debug statement to confirm that I am successfully talking to the api, and it does get lat long back.
The website is http://americanhorseproperties.com
<div id="map">
</div>
<a asp-page="./Index">Back to Gallery</a>
<input type="hidden" id="address" value="#Model.house.address
#Model.house.city" />
<script>
// Initialize and add the map
function initMap() {
var geocoder = new google.maps.Geocoder();
//gets address from the address hidden input element in body
var address = document.getElementById('address').value;
//geocodes address and gets latlong
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == 'OK') {
console.log('location: Longitude: ',
results[0].geometry.location.lng());
console.log('location: Latitude: ',
results[0].geometry.location.lat());
var map = new google.maps.Map(document.getElementById('map'), { zoom: 14,
center: results[0].geometry.location });
var marker = new google.maps.Marker({ map: map, position:
results[0].geometry.location }); //sets marker at new lat long
} else {
alert('Geocode was not successful for the following reason: ' +
status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?
key=KEY_REMOVED&callback=initMap">
</script>
It seems you need to set "height" property for <div id="map">
CSS Problem.
The Google Maps API automatically adds the CSS properties "position: relative; overflow: hidden;" to the div that is used for displaying the map. When I manually remove any of these two properties in the developer console of the browser, the map becomes visible.
Make sure the div has a size that the map can fill.

Identification of script that is being used for maps

I want to create meetup groups in different countries for my mental health support website.
I've found a site that display a map within a wordpress page (http://www.cleargreen.com/community/practice-groups) where people can create events and manage them themselves, it uses a third party script you have to login to (https://www.cleargreen.com/english/tensegrity/login.cfm?dsplyLanguageID=1).
I was wondering what script they are using? and how did they integrate it so well with wordpress?
You can use google maps api located on https://maps.googleapis.com/maps/api/js
And then in wordpress you are probably already using ACF plugin(if not http://www.advancedcustomfields.com/)
You can create custum field for google maps OR fields with latitude and longitude
Then in your them add
<?php $location = get_field("[YOUR FIELD NAME]", $post->ID); ?>
<script>
function initialize() {
var map_canvas = document.getElementById("[YOURE CONTAINER ID]");
var map_options = {
center: new google.maps.LatLng(<?php echo $location["lat"] ?>, <?php echo $location["lng"] ?>),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
var marker = new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $location["lat"] ?>, <?php echo $location["lng"] ?>),
map: map,
icon: '[PATH TO YOUR PIN IMAGE]'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

InfoWindows on google map

Does i can do that kind of google infoWindows with the infoWindows function from google map api 3 when i click on the marker ?
http://i40.tinypic.com/25unplc.png
var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
I am not even able to do a line break in those information windows...
Add html code to your infowindow to apply styles like:
var contentString = '<div id="content">'+
'<h2>Meta Creation</h2><br/>'+
'<div id="bodyContent">'+
' '+
'</div>'+
'</div>';
infowindow.setContent(contentString);
NOTE: Just used some of the html tags to show example. Change according to your requirement.

Google Maps 3.x with jQuery UI 1.10.x issue

I have created a test ASP.Net 4.0 application with Master \ Child page configuration to test some needed enhancements for our main application. I need to be able to put a google map inside a jQuery UI tab control. It will not be the first tab in the production version so I create a tab control with two tabs in the test app and put the map in the second tab. No matter what I did I could not get the map to display correctly in the second tab. As a test I move the map (just a div id="map-canvas" that jQuery attacks) into the first tab and walla - everything works fine - even after changing tabs and returning. Googled around a ton but cannot find a resolution for this one. Here is the map working in the first tab:
And here is the same map not working when placed in the second tab:
Here is the code:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=MyAPIKey&sensor=false">
</script>
<link href="App_Themes/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>Map in Tab Control</h2>
<div id="mapTabs">
<ul>
<li>Tab-1</li>
<li>Tab-2</li>
</ul>
<div id="a"><h2>This is the first tab</h2>
</div>
<div id="b"><h2>This is the second tab</h2>
<div id="map-canvas" style="min-width:300px; min-height:300px;"></div>
</div>
</div>
<script src="Scripts/jquery.ui.core.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.widget.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.tabs.min.js" type="text/javascript"></script>
<script type="text/javascript">
function initialize(lt, ln) {
var latlng = new google.maps.LatLng(lt, ln);
var map = new google.maps.Map(
document.getElementById('map-canvas'), {
center: new google.maps.LatLng(lt, ln),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latlng,
title: "Test Pin"
});
marker.setMap(map);
}
$(document).ready(function () {
$("#mapTabs").tabs();
var addr = '2626 East Oakland Park Blvd Oakland Park, FL 33306';
var gc = new google.maps.Geocoder();
var lat;
var lon;
gc.geocode({ 'address': addr }, function (results, status) {
lat = results[0].geometry.location.lat();
lon = results[0].geometry.location.lng();
initialize(lat, lon);
});
});
</script>
</asp:Content>
I know this is an old post, but here's for anybody else with similar issues:
You'll need to call your map's initialize function.
Since you're using jquery, this should work:
$("#b").click(function() {
initialize();
});

Google maps: infowindow is not closing

My infowindows are not closing automatically when i click on the other marker..
here the code : http://pastebin.com/PvCt2z7W
here is the code for markers with infowindows
for (var n = 0 ; n <listFavourite.length ; n++)
//var favouriteObject =listFavourite[n];
addMarker(listFavourite[n]);
}
function addMarker(data) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lattitude, data.longitude),
map: map,
title: data.address
});
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h2 id="firstHeading" class="firstHeading">'+data.name+'</h2>'+
'<div id="bodyContent">'+
'<p>'+data.address+'</p>'+
'<p></p>'+
'<p>Do You Want to change search location</p>'+
'<input name="yes" id="yes" type="button" class="btn-common" value="Yes"/>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
maxWidth: 10,
});
google.maps.event.addListener(marker, "click", function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
};
i tried all the answers here but not able to work it properly
Any suggestions or help Would be appreciated
Instead of creating one infowindow for each marker, have one global infowindow variable. Then all you're doing in your marker click handler is updating the content for the marker each time. However your code will require this to be done with a closure, otherwise you're going to get each marker having the last marker's content.
var infowindow = new google.maps.InfoWindow({
maxWidth: 10
});
var arrMarkers = [];
function addMarker(data) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lattitude, data.longitude),
map: map,
title: data.address
});
arrMarkers.push(marker);
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h2 id="firstHeading" class="firstHeading">'+data.name+'</h2>'+
'<div id="bodyContent">'+
'<p>'+data.address+'</p>'+
'<p></p>'+
'<p>Do You Want to change search location</p>'+
'<input name="yes" id="yes" type="button" class="btn-common" value="Yes"/>'+
'</div>'+
'</div>';
// add an event listener for this marker
bindInfoWindow(marker, map, infowindow, contentString);
}
function bindInfoWindow(marker, map, infowindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
}
function clickLink(ID) {
google.maps.event.trigger(arrMarkers[ID], 'click');
}
Your HTML of sidebar links might look like:
Link 1<br>
Link 2<br>
...
The other option to using function closure (which basically involves using a createMarker function) is to save the content of the infowindow in a property of the marker. It still is simplest with a single infowindow. You haven't provided enough context in your pastebin to give you a working example that does that.
Here is an example that uses function closure to associate the infowindow content with the marker and has a single infowindow (translated from Mike Williams' v2 tutorial).
http://www.geocodezip.com/v3_MW_example_map3.html
Mike Williams' description of function closure
Here is an example that doesn't use function closure (it stores the infowindow content in a property of the marker object):
http://www.geocodezip.com/v3_MW_example_map3_noclosure.html

Resources