AngularJS Google Maps Search/Auto-Complete - google-maps

I am using Angular UI's Google Maps directives.
I would like to use a text box for the user to enter a location and with auto-complete, load the location in the maps canvas when user selects from auto-complete drop down.
How would i go about doing that using these directives ?
Thanks

This feature is not supported yet and is under construction. Refer: https://github.com/nlaplante/angular-google-maps/issues/383

The auto-complete location is supported by Angular UI Google Maps directives
Here's your search textbox and map canvas.
<input id="pac-input" class="controls" type="text" placeholder="Search for a location" ng-model="model.searchAddress" />
<div ui-map="model.locationMap" ui-options="mapOptions" id="map_canvas" class="map"
style="height: 420px; width: 420px;" ui-event="{'map-click': 'setMarker($event, $params)'}"></div>
Here's JS code that you should put into controller associated with your map and search textbox. This code taps into Google Maps API,
var searchAddressInput = document.getElementById('pac-input');
var mapBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(53.5085011, -6.2154598), //south east corner
new google.maps.LatLng(53.5585011, -6.2654598) //north west corner
);
var mapOptions = {
center: new google.maps.LatLng(53.5085011, -6.2154598),
zoom: 9,
mapTypeId: google.maps.MapTypeId.ROADMAP,
bounds: mapBounds
};
var autocomplete = new google.maps.places.Autocomplete(searchAddressInput, mapOptions);
//set the bounds of autocomplete to map's current viewport
autocomplete.bindTo('bounds', $scope.model.locationMap);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
onPlaceChanged();
});
In the code above you are associating autocomplete object with your searchbox, then binding the onPlaceChanged() function to it, once one of the suggested locations is clicked.
This is the code that will be executed
function onPlaceChanged() {
var place = autocomplete.getPlace();
$scope.model.locationMap.panTo(place.geometry.location);
$scope.model.locationMap.setZoom(14);
//place pin on the map
marker.setPosition(new google.maps.LatLng(place.geometry.location.k, place.geometry.location.B));
}
That would be all. One more word of advice though. If you happen to be using an autocomplete for a Google Map in the modal window, you have to add this snippet into your CSS:
div.pac-container {
z-index: 1050 !important;
}
Bootstrap modal has a z-index of 900 something, so the autocomplete suggestions would appear behind it. You wouldn't get any JS errors, so it's not an ideal situation, cause you donn't know what the hell is going on. Changing z-index ensures that autocomplete suggestions are on the top.

Related

Positioning two Google Map frames next to each other [duplicate]

This question already has answers here:
How to Display Multiple Google Maps per page with API V3
(11 answers)
Closed 7 years ago.
I'm developing a contact page and having a bit of an issue with displaying two google map frames next to each other. I got the code from the Google developer website.
HTML & CSS:
<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script><div style='overflow:hidden;height:440px;width:550px;'><div id='gmap_canvas' style='height:440px;width:550px;'></div><div><small> embed google map </small></div><div><small>generate Google Maps</small></div><style>#gmap_canvas img{max-width:none!important;background:none!important}</style></div><script type='text/javascript'>function init_map(){var myOptions = {zoom:10,center:new google.maps.LatLng(50.85191589999999,0.5627325000000383),mapTypeId: google.maps.MapTypeId.ROADMAP};map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);marker = new google.maps.Marker({map: map,position: new google.maps.LatLng(50.85191589999999,0.5627325000000383)});infowindow = new google.maps.InfoWindow({content:'<strong>1066 Kung Fu School</strong><br>26 Grand Parade, St Leonards on Sea<br>'});google.maps.event.addListener(marker, 'click', function(){infowindow.open(map,marker);});infowindow.open(map,marker);}google.maps.event.addDomListener(window, 'load', init_map);</script>
<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script><div style='overflow:hidden;height:440px;width:550px;'><div id='gmap_canvas' style='height:440px;width:550px;'></div><div><small> embed google map </small></div><div><small>googlemapsgenerator.com</small></div><style>#gmap_canvas img{max-width:none!important;background:none!important}</style></div><script type='text/javascript'>function init_map(){var myOptions = {zoom:10,center:new google.maps.LatLng(50.85191589999999,0.5627325000000383),mapTypeId: google.maps.MapTypeId.ROADMAP};map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);marker = new google.maps.Marker({map: map,position: new google.maps.LatLng(50.85191589999999,0.5627325000000383)});infowindow = new google.maps.InfoWindow({content:'<strong> St Mary Magdalene’s Hall</strong><br>Magdalene Road, Bexhill on Sea<br>'});google.maps.event.addListener(marker, 'click', function(){infowindow.open(map,marker);});infowindow.open(map,marker);}google.maps.event.addDomListener(window, 'load', init_map);</script>
In case its hard to see the CSS (its inline) styles:
max-width:none!important;background:none!important;float:left!important;
max-width:none!important;background:none!important;float: right!important;
Screenshot of how it looks and what I need:
[1]http://i.imgur.com/JmO3d4Z.png "screenshot"
Cheers.
Please look at this JSFiddle - Two maps on the same page.
Note how he creates two divs, inline-styling them, and doing the JavaScript work that applies on the divs separately (rather than the example provided by you).
<div id="map_canvas" style="top: 10px; left: 25px; width:210px; height:220px; float: left"></div>
<div id="map_canvas_2" style="top: 10px; left: 75px; width:210px; height:220px"></div>
And then, using JS:
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var map2 = new google.maps.Map(document.getElementById("map_canvas_2"), myOptions2);

Center and enlarge google maps

This is the code:
#map_canvas {
height: 1000px;
text-align: center;
}
<div dojoType="dojox.mobile.ContentPane" style="overflow: auto;">
<div id="map_canvas"></div>
<!-- cambiare style -->
</div>
I tried to center with various css attributi but don't works.How i center and enlarge all map in panel?
You have to resize your map.
Try this
var mapOptions = {
center: new google.maps.LatLng(43.653226, -79.3831843),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
$('#map-canvas').fadeIn('slow', function() {
google.maps.event.trigger(map, 'resize');
}
Anish's code is working for me. (minor fix: map-canvas -> map_canvas) The map fills the width of the page and is 1000px high.
Looking at the screenshot, your map is filling the width of the window. You can tell this because the map control is all the way to the right of the screen in the upper corner. But for some reason, the map isn't fetching the tiles that it needs to fill itself. This usually happens when the map container is resized without triggering the google map resize event. Are you creating the map before the dojo parser runs? I put Anish's code inside the ready() function in dojoInit() and the map fills the screen when it is shown.
Sizing google maps to fill available space (not using absolute height and width) is tricky, but usually the problem is the height, not the width. I've written a series of blog post on google maps in worklight here: http://www.ibm.com/developerworks/community/blogs/dhuyvett

Google Map Gets Cropped When Using Display:Inherit;

I am trying to hide/show Google Maps by switching between display:none; and display:inherit;. When I apply display:inherit to Google Maps, part of the map gets cropped and I am unsure why. How would I fix this while still using display:none;?
Link to example
//HTML
<button id="toggleMap">Toggle Google Map</button>
<button id="toggleImage">Toggle Image</button>
<div id="image">
<img src="http://jdm-digital.com/wp-content/uploads/2012/04/google-jquery-opt-465x346.jpg">
</div>
<div id="map" style="width:500px; height:500px; display:none;">
<div id="map_canvas" style="height:100%;"></div>
</div>
//JavaScript
$('#toggleImage').click(function() {
$('#image').css('display', 'inherit');
$('#map').css('display', 'none');
});
$('#toggleMap').click(function() {
$('#image').css('display', 'none');
$('#map').css('display', 'inherit');
});
If you create a map while it's hidden the API doesn't know how big it should be because the browser reports it has zero size. The API only loads enough tiles for a zero-size map, and positions the centre at (0,0) — the top-left corner.
When you unhide the map, tell the API to get the new size by triggering the resize event:
google.maps.event.trigger(map,'resize')
Documentation — scroll down to Events.
If i understand correctly you can give display:block instead of display:inherit
My solution was to use $('#map').append(mapHTML); whenever #toggleMap was clicked. This way allowed for the map to be created whenever #map becomes visible.
I feel that this approach is sort of hack-ish; If anyone has an alternative solution I'd definitely like to know about it.
//HTML
<div id="map" style="width:500px; height:500px; display:none;"></div>
//JavaScript
$('#toggleMap').click(function() {
$('#image').css('display', 'none');
$('#map').css('display', 'inherit');
var mapHTML = "<div id=\"map_canvas\" style=\"height:100%;\"></div>";
$('#map').append(mapHTML);
googleMap(); //Google Map's initialize() that would create the map
});

How to disable mouse wheel zoom on Google streetview?

Consider the following example code:
http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html
I can do scrollwheel: false on a mapOptions to disable mouse wheel zoom. The feature is not implemented on the streetview panoramaOptions.
In Chrome, I can safely disable mouse wheel zoom on the streetview simply by.
$('#pano *').bind('mousewheel', function(){
return false;
});
However this does not work in elsewhere. Please help me get it working in top 3-5 browsers?
There is now a confirmed feature request with Gmaps API issues http://code.google.com/p/gmaps-api-issues/issues/detail?id=2557. Let's hope Google's engineers will look into it when they get trough more important bugs. I hate to hold my breath, but I'll park this for now.
According to Subgurim.net, this is how to do it;
[powerscript - other event]
CONSTANT integer WM_MOUSEWHEEL = 522
IF message.number = WM_MOUSEWHEEL AND & KeyDown (KeyControl!) THEN
message.processed = TRUE
RETURN 1
END IF
I realise that you are using Javascript, and this is a VB example, but hopefully it helps.
I use the below code to avoid double click zoom - pretty sure it could be adapted to scroll wheel zoom. zoom: 1 being my preferred zoom stage here, but could be anything.
$("#panorama").dblclick( function(){
panorama.setPov( {
heading: panorama.getPov().heading,
pitch: panorama.getPov().pitch, zoom: 1
});
});
There is an easy workaround: by adding a layer before the streetview with a higher z-index, you will prevent the zooming function of the Streetview.
<div id="panorama" style="position: absolute;left:0; top: 0;height: 100%; width: 100%; z-index: -1;opacity: 1"></div>
<div id="layerBeforePanorama" style="position: absolute;left:0; top: 0;height: 100%; width: 100%; z-index: 0;opacity: 1"></div>
<script>
var panoOptions = {
position: latlng,
pov: {
heading: 0,
pitch: 0
}
};
var pano = new google.maps.StreetViewPanorama(document.getElementById('panorama'), panoOptions);
</script>

Google Maps v3, custom control with textbox: can't perform input

I am trying to add a custom control to my google map (v3): I want to have search box just near the other map controls. So I add a div with <input type="textbox"> on it, and it's being shown on the map. But the problem is that the textbox is inaccessible: I can't type anything inside it, or even focus on it.
function SearchBox() {
var searchTextBox = $('<input type="text" id="txtGeoSearch" />');
var div = $('<div class="geoSearchBox"></div>')
.append($('<span>Search:</span>'))
.append(searchTextBox);
return div.get(0);
}
// inside init()
geoMap.controls[google.maps.ControlPosition.TOP_LEFT].push(new SearchBox());
So what should I do to make my textbox behave like normal one?
Works for me: http://jsfiddle.net/cFpkt/