How do I position my Google Map search box inside my Bootstrap 4 navbar?












-1














I would like to place the Google Maps search box inside of the gray navbar at the top of my page. However, I can't seem to figure out the balance between styling and Google Maps API to pull the search box away from the map.



Here's what I have for my JavaScript



 //Initialize map function
function initAutocomplete() {

//setting variable for location of Portland, ME by latitude and longitude coordinates
var portland = {
lat: 43.6591,
lng: -70.2568
};

//Centering map in portland
map = new google.maps.Map(document.getElementById('map'), {
center: portland,
zoom: 15,
mapTypeId: 'roadmap'
});

//Links searchbox in UI to javascript functionality
var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)

//Biases searchBox results towards map's viewport (centered at portland)
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = ;
//Listener for when user selects a predicted text to get more details
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}

//Clears out old markers
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = ;

//For each result, get the name of each location
var bounds = portland;
places.forEach(function(place) {
if (!place.geometry) {
console.log("No results available at this time");
return;
}

//Creates Marker for each place
markers.push(new google.maps.Marker({
map: map,
label: place.name,
position: place.geometry.location
}));
});
//fits map within bias bounds
map.fitBounds(bounds)
})
}


Here's what I have for HTML:



    <div class="container-fluid">
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Where you want to eat</a>
<form class="form-inline">
<input id="mapSearch" class="controls" type="text" placeholder="Search">
</form>
</nav>

<!--div holding google maps-->
<div id="map"></div>
</div>


I know the issue lies in this portion of the code:



var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)


I've tried all of the following methods to no avail. Some of these methods move the search box to the navbar as I want it to, but then that throws an "Uncaught (in promise) TypeError" in the console which then stops the entire thing from functioning:




  • commenting out "var input" altogether

  • passing the "mapSearch" element ID through the push function

  • removing the "[google.maps.ControlPosition.TOP_LEFT]" portion of the code, as well as changing with the position property if not eliminating it altogether

  • identifying the "mapSearch" element through jQuery instead of using document.getElementById
    -CSS styling the #mapSearch ID through relative and absolute positions, and changing the top and left/right properties with those
    -removing the entire "map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)" altogether


I apologize if my wording isn't correct. I'm a relatively new coder, and this is something I'm building out in the second round of an interview for a jr. dev position. I've tried searching through Stack Overflow and through the Google API over the last few days and I'm completely stuck.










share|improve this question




















  • 1




    Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
    – geocodezip
    Nov 23 at 2:45












  • jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
    – Shelby
    Nov 23 at 3:31








  • 1




    I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
    – geocodezip
    Nov 23 at 10:49


















-1














I would like to place the Google Maps search box inside of the gray navbar at the top of my page. However, I can't seem to figure out the balance between styling and Google Maps API to pull the search box away from the map.



Here's what I have for my JavaScript



 //Initialize map function
function initAutocomplete() {

//setting variable for location of Portland, ME by latitude and longitude coordinates
var portland = {
lat: 43.6591,
lng: -70.2568
};

//Centering map in portland
map = new google.maps.Map(document.getElementById('map'), {
center: portland,
zoom: 15,
mapTypeId: 'roadmap'
});

//Links searchbox in UI to javascript functionality
var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)

//Biases searchBox results towards map's viewport (centered at portland)
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = ;
//Listener for when user selects a predicted text to get more details
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}

//Clears out old markers
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = ;

//For each result, get the name of each location
var bounds = portland;
places.forEach(function(place) {
if (!place.geometry) {
console.log("No results available at this time");
return;
}

//Creates Marker for each place
markers.push(new google.maps.Marker({
map: map,
label: place.name,
position: place.geometry.location
}));
});
//fits map within bias bounds
map.fitBounds(bounds)
})
}


Here's what I have for HTML:



    <div class="container-fluid">
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Where you want to eat</a>
<form class="form-inline">
<input id="mapSearch" class="controls" type="text" placeholder="Search">
</form>
</nav>

<!--div holding google maps-->
<div id="map"></div>
</div>


I know the issue lies in this portion of the code:



var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)


I've tried all of the following methods to no avail. Some of these methods move the search box to the navbar as I want it to, but then that throws an "Uncaught (in promise) TypeError" in the console which then stops the entire thing from functioning:




  • commenting out "var input" altogether

  • passing the "mapSearch" element ID through the push function

  • removing the "[google.maps.ControlPosition.TOP_LEFT]" portion of the code, as well as changing with the position property if not eliminating it altogether

  • identifying the "mapSearch" element through jQuery instead of using document.getElementById
    -CSS styling the #mapSearch ID through relative and absolute positions, and changing the top and left/right properties with those
    -removing the entire "map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)" altogether


I apologize if my wording isn't correct. I'm a relatively new coder, and this is something I'm building out in the second round of an interview for a jr. dev position. I've tried searching through Stack Overflow and through the Google API over the last few days and I'm completely stuck.










share|improve this question




















  • 1




    Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
    – geocodezip
    Nov 23 at 2:45












  • jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
    – Shelby
    Nov 23 at 3:31








  • 1




    I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
    – geocodezip
    Nov 23 at 10:49
















-1












-1








-1







I would like to place the Google Maps search box inside of the gray navbar at the top of my page. However, I can't seem to figure out the balance between styling and Google Maps API to pull the search box away from the map.



Here's what I have for my JavaScript



 //Initialize map function
function initAutocomplete() {

//setting variable for location of Portland, ME by latitude and longitude coordinates
var portland = {
lat: 43.6591,
lng: -70.2568
};

//Centering map in portland
map = new google.maps.Map(document.getElementById('map'), {
center: portland,
zoom: 15,
mapTypeId: 'roadmap'
});

//Links searchbox in UI to javascript functionality
var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)

//Biases searchBox results towards map's viewport (centered at portland)
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = ;
//Listener for when user selects a predicted text to get more details
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}

//Clears out old markers
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = ;

//For each result, get the name of each location
var bounds = portland;
places.forEach(function(place) {
if (!place.geometry) {
console.log("No results available at this time");
return;
}

//Creates Marker for each place
markers.push(new google.maps.Marker({
map: map,
label: place.name,
position: place.geometry.location
}));
});
//fits map within bias bounds
map.fitBounds(bounds)
})
}


Here's what I have for HTML:



    <div class="container-fluid">
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Where you want to eat</a>
<form class="form-inline">
<input id="mapSearch" class="controls" type="text" placeholder="Search">
</form>
</nav>

<!--div holding google maps-->
<div id="map"></div>
</div>


I know the issue lies in this portion of the code:



var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)


I've tried all of the following methods to no avail. Some of these methods move the search box to the navbar as I want it to, but then that throws an "Uncaught (in promise) TypeError" in the console which then stops the entire thing from functioning:




  • commenting out "var input" altogether

  • passing the "mapSearch" element ID through the push function

  • removing the "[google.maps.ControlPosition.TOP_LEFT]" portion of the code, as well as changing with the position property if not eliminating it altogether

  • identifying the "mapSearch" element through jQuery instead of using document.getElementById
    -CSS styling the #mapSearch ID through relative and absolute positions, and changing the top and left/right properties with those
    -removing the entire "map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)" altogether


I apologize if my wording isn't correct. I'm a relatively new coder, and this is something I'm building out in the second round of an interview for a jr. dev position. I've tried searching through Stack Overflow and through the Google API over the last few days and I'm completely stuck.










share|improve this question















I would like to place the Google Maps search box inside of the gray navbar at the top of my page. However, I can't seem to figure out the balance between styling and Google Maps API to pull the search box away from the map.



Here's what I have for my JavaScript



 //Initialize map function
function initAutocomplete() {

//setting variable for location of Portland, ME by latitude and longitude coordinates
var portland = {
lat: 43.6591,
lng: -70.2568
};

//Centering map in portland
map = new google.maps.Map(document.getElementById('map'), {
center: portland,
zoom: 15,
mapTypeId: 'roadmap'
});

//Links searchbox in UI to javascript functionality
var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)

//Biases searchBox results towards map's viewport (centered at portland)
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = ;
//Listener for when user selects a predicted text to get more details
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}

//Clears out old markers
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = ;

//For each result, get the name of each location
var bounds = portland;
places.forEach(function(place) {
if (!place.geometry) {
console.log("No results available at this time");
return;
}

//Creates Marker for each place
markers.push(new google.maps.Marker({
map: map,
label: place.name,
position: place.geometry.location
}));
});
//fits map within bias bounds
map.fitBounds(bounds)
})
}


Here's what I have for HTML:



    <div class="container-fluid">
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Where you want to eat</a>
<form class="form-inline">
<input id="mapSearch" class="controls" type="text" placeholder="Search">
</form>
</nav>

<!--div holding google maps-->
<div id="map"></div>
</div>


I know the issue lies in this portion of the code:



var input = document.getElementById("mapSearch");
var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)


I've tried all of the following methods to no avail. Some of these methods move the search box to the navbar as I want it to, but then that throws an "Uncaught (in promise) TypeError" in the console which then stops the entire thing from functioning:




  • commenting out "var input" altogether

  • passing the "mapSearch" element ID through the push function

  • removing the "[google.maps.ControlPosition.TOP_LEFT]" portion of the code, as well as changing with the position property if not eliminating it altogether

  • identifying the "mapSearch" element through jQuery instead of using document.getElementById
    -CSS styling the #mapSearch ID through relative and absolute positions, and changing the top and left/right properties with those
    -removing the entire "map.controls[google.maps.ControlPosition.TOP_LEFT].push(input)" altogether


I apologize if my wording isn't correct. I'm a relatively new coder, and this is something I'm building out in the second round of an interview for a jr. dev position. I've tried searching through Stack Overflow and through the Google API over the last few days and I'm completely stuck.







javascript html google-maps-api-3 bootstrap-4 google-places-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 10:47









geocodezip

125k10141171




125k10141171










asked Nov 23 at 0:09









Shelby

1




1








  • 1




    Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
    – geocodezip
    Nov 23 at 2:45












  • jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
    – Shelby
    Nov 23 at 3:31








  • 1




    I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
    – geocodezip
    Nov 23 at 10:49
















  • 1




    Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
    – geocodezip
    Nov 23 at 2:45












  • jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
    – Shelby
    Nov 23 at 3:31








  • 1




    I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
    – geocodezip
    Nov 23 at 10:49










1




1




Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
– geocodezip
Nov 23 at 2:45






Removing this line: map.controls[google.maps.ControlPosition.TOP_LEFT].push(input) works for me (fiddle). Please provide a Minimal, Complete, and Verifiable example that demonstrates your issue.
– geocodezip
Nov 23 at 2:45














jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
– Shelby
Nov 23 at 3:31






jsfiddle.net/#&togetherjs=kiAlNLsObu I'm stumped because I copied and pasted the code into jsfiddle and can't get it to render anything.
– Shelby
Nov 23 at 3:31






1




1




I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
– geocodezip
Nov 23 at 10:49






I don't see anything useful in that fiddle. Please provide a Minimal, Complete, and Verifiable example in the question itself (preferably a StackOverflow code snippet), not (just) a link to an external site (like jsfiddle).
– geocodezip
Nov 23 at 10:49



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439295%2fhow-do-i-position-my-google-map-search-box-inside-my-bootstrap-4-navbar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439295%2fhow-do-i-position-my-google-map-search-box-inside-my-bootstrap-4-navbar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)