Google Map Places selection conflicts with marker selection on iOS - ios

I currently have a google map with a places select box.
I currently have a (click) binding on the map, when it's clicked in a certain location, a marker will be placed on that location.
Now I also have an input field with places so you can search for specific areas. The problem is now that when you search for a spot, and then press the location, the location will not be picked but instead a marker will show on the area you tapped.
(looks like a z-index issue but the input is actually above the map).
This issue only occurs on iOS devices. Android and Windows work fine
Edit: setting .pac-container { z-index: 999999 !important; }
Code:
Html
<input *ngIf="!isStaticLocation" type="text" class="controls" #pacInput placeholder="Search Box"/>
<div #googleMap id="map" style="width: 100%; height: 300px;"></div>
TS (Simplified)
// init the google map
initMap() {
var centerOfMap;
var draggableMarker = true;
centerOfMap = new google.maps.LatLng(this.locationService.gpsLat, this.locationService.gpsLong);
var options = {
center: centerOfMap,
zoom: 11,
fullscreenControl: false,
disableDefaultUI: true, // dont allow default zoom/sattelite/street view
gestureHandline: 'cooperative' // disable scrolling conflict of the page
};
this.map = new google.maps.Map(this.googleMap.nativeElement, options);
this.marker = new google.maps.Marker({
position: centerOfMap,
map: this.map,
draggable: draggableMarker
});
var searchBox = new google.maps.places.SearchBox(this.googleInput.nativeElement);
// add the searchbar to the google map
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(this.googleInput.nativeElement);
// Bias the SearchBox results towards current map's viewport
this.map.addListener('bounds-changed', () => {
searchBox.setBounds(this.map.getBounds());
});
searchBox.addListener('places_changed', () => {
var places = searchBox.getPlaces();
if(places.length == 0) {
return;
}
var bounds = new google.maps.LatLngBounds();
places.forEach(place => {
if(!place.geometry) {
console.log("returned place contains no geometry");
return;
}
this.setMarkerLocation(place);
if(place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extends(place.geometry.location);
}
});
this.map.fitBounds(bounds);
});
google.maps.event.addListener(this.map, 'click', (event: any)=> {
var clickedLocation = event.latLng;
this.marker.setPosition(clickedLocation);
this.getMarkerLocation();
});
}
// function to set the location marker on a different spot
setMarkerLocation(place: any) {
this.marker.setMap(null);
this.marker = new google.maps.Marker({
position: place.geometry.location,
map: this.map,
draggable: true
});
this.getMarkerLocation();
}
// updates configuration of the app.
getMarkerLocation() {
var currLoc = this.marker.getPosition();
this.locationService.setGoogleMapsLocation(currLoc.lat(), currLoc.lng());
this.locationChanged = true;
}

Related

google maps markerwithlabel not displaying in mvc

I'm not exactly sure on how to use this external library, it seems I need to make another request to get the markers to show up but I'm not sure of the best way to do it in mvc.
Error I am getting is: MarkerLabel_.getSharedCross is not a function
Points just contains an array of Lats/Lngs
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: points[0],
zoom: 10
});
for (var i = 0; i < points.length; i++) {
var marker = new MarkerWithLabel({
position: points[i],
draggable: true,
map: map,
labelContent: "$425K",
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: {opacity: 0.75},
icon: "../Content/Images/map-icon.png"
});
marker.setMap(map);
}
}
<script src="https://maps.googleapis.com/maps/api/js?key=MyKey&callback=initMap"
async defer></script>
<script src="~/Scripts/Custom/marker-with-label.js"></script>

Cannot read property 'offsetWidth' of null, google maps api js

I need to initialize load map on page click, but I have error. On my page can be more that one map, and thats why i do initialization on page click. This is my code. Please help me to solve this issue.
$('.button-expand').on('click', function() {
var self = $(this).text();
if (self === 'EXPAND') {
var location = #item.Latitude;
if (location != null) {
var latlng = new google.maps.LatLng(#item.Latitude, #item.Longitude);
var mapOptions = {
center: latlng,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var name = '#gmap-' + #item.Id;
var map = new google.maps.Map(document.getElementById(name), mapOptions);
// create a marker
var image = '../Content/Images/map_icon.png';
var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: image
});
}
}
});
This is div:
#if (item.Latitude != null)
{
<p>Map:</p>
<div class="map-margin" id="gmap-#item.Id" data-id="#item.Id" style="min-height: 270px; height: 270px; min-width: 470px; width: 470px"></div>
}

trigger a marker click

I am using a Fusion Table and the Google Map API and would like to know if I can automatically trigger an event when a marker is placed.
Currently I can click on the map and I get a customized infowindow, but I want to do the same thing but after geocoding and placing a marker... I would like this to happen automatically.
Here is my code so far:
var FusionTableID = '<?php echo get_theme_mod( 'fusion_id' )?>';
var map = null;
var geocoder = null;
var marker = null;
function initialize() {
geocoder = new google.maps.Geocoder();
var local = new google.maps.LatLng(<?php echo get_theme_mod( 'lat-long' )?>);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: local,
zoom: <?php echo get_theme_mod( 'zoom' )?>
});
// forces a refresh to from Fusion Tables
//var and=' geometry does not contain "'+new Date().getTime()+'"';
var layer = new google.maps.FusionTablesLayer({
//suppressInfoWindows: true,
query: {
select: "col2\x3e\x3e0",
from: FusionTableID,
where: ""
},
options: {
styleId: 2,
templateId: 2
},
map: map
});
layer.setMap(map);
google.maps.event.addListener(layer, "click", function(e) {
var point = e.latLng.toUrlValue(6);
var latlngStr = point.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
if (marker && marker.setMap) marker.setMap(null);
marker = new google.maps.Marker({
position: latlng,
map: map
});
console.log(e.row['objectid'].value);
var tname = e.row['name'].value; // name
var targetDist = e.row['description'].value; // description
// poplulate the infowindow
e.infoWindowHtml = "<div class=\"grid-100\"><h2>"+tname+"</h2>"+targetDist+"<hr></div>";
});
}
function showAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (marker && marker.setMap) marker.setMap(null);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
if (results[0].geometry.viewport)
map.fitBounds(results[0].geometry.viewport);
// THIS DOESN'T WORK ****
new google.maps.event.trigger( marker, 'click' );
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
Thanks geocodezip
It isn't really possible to "trigger" a click on a FusionTablesLayer (you can, but you basically need to replicate the information in the click event, which is painful), you can query the table an create your own infowindow on the marker though

google map displays fine first time; partial display after adding new marker

I'm using google maps and jquery mobile.
When the user touches a button, a map displays.
There are markers on this map, they were already known when the user touched the map display button.
This works fine. Here's the html:
<div data-role="page" style="width: 100%; height: 100%" data-ajax="false" id="map-content">
<div id="map_canvas" class="ui-content" style="width: 100%; height: 100%"></div>
</div>
Here's the javascript:
function showMap(){
var initLoc;
console.log("Pend showMap() START");
$.mobile.changePage( "#map-content", { transition: "slideup"} );
$(document).on('pageshow', '#map-content',function(e,data){
$('#map-content').height(getRealContentHeight());
initLoc = new google.maps.LatLng(0,0);
$('#map_canvas').gmap( { 'center': initLoc, 'zoom' : 16 } );
$('#map_canvas').gmap('addMarker', {'position': initLoc,
'icon': gimage,
'shadow' : shadow,
'bound': false});
for (var i = 0; i<myArr.length; i++){
var newLoc = new google.maps.LatLng(myArr[i][1], myArr[i][2]);
$('#map_canvas').gmap('addMarker', {'id':i,
'position': newLoc,
'icon': bimage,
'shadow' : shadow,
'bound': false}).click(function() {
$('#map_canvas').gmap('openInfoWindow',
{ 'content': 'some content },
this);
});
}
google.maps.event.trigger($('#map_canvas'), 'resize');
google.maps.event.trigger($('#map-content'), 'resize');
});
}
function getRealContentHeight(){
var header = $.mobile.activePage.find("div[data-role='header']:visible");
var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
var content = $.mobile.activePage.find("div[data-role='content']:visible:visible");
var viewport_height = $(window).height();
var content_height = viewport_height - header.outerHeight() - footer.outerHeight();
if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) {
content_height -= (content.outerHeight() - content.height());
}
return content_height;
}
The problem comes when the user leaves this page with:
$.mobile.changePage("#main", {
transition: 'slide',
reverse: true });
then adds a new marker to the map and tries to redisplay it by calling showMap()
What displays is a partial map with mostly blank space.
I've read this is a symptom of google maps not knowing the size of the screen to display, but the solution is supposed to be:
google.maps.event.trigger($('#map_canvas'), 'resize');
google.maps.event.trigger($('#map-content'), 'resize');
Can anyone suggest the proper way to add markers to a previously rendered map and then redisplay the map?
This doesn't look correct to me:
google.maps.event.trigger($('#map_canvas'), 'resize');
google.maps.event.trigger($('#map-content'), 'resize');
The first argument for google.maps.event.trigger needs to be a Google Maps Javascript API v3 object, in this case a google.maps.Map object.
The JQuery selector $('#map_canvas') does not return a google.maps.Map.
To get the required google.maps.Map, use: $('#map_canvas').gmap('getMap')
This should work for you:
google.maps.event.trigger($('#map_canvas').gmap('getMap'), 'resize');
(can't tell from your code whether there is a map on "map-content" or not, if so, you need to do the same thing there)

Displaying markers on Google Map with the help of several longitude' and latitude's

I want to display several placemarkers on the map with the help of lat's and long's that I have in my controller. There are no build errors and I am also able to see the map with the various placemarker but only the last info window is responding to the click event.
#section Script {
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
}
<div id="map_canvas" style="width: 875px; height: 600px">
</div> `
<script type="text/javascript">
//display the map
var stockholm = new google.maps.LatLng(59.32522, 18.07002);
var myOptions = {
zoom: 4,
center: stockholm,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//display markers
#foreach (var item in Model)
{
<text>
var latLng = new google.maps.LatLng('#(item.Latitude)', '#(item.Longitude)');
var title = '#(item.Title)';
var contentString = '<h3>' + title + '</h3>'
var marker = new google.maps.Marker({
position: latLng,
map: map
});
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function () {
infoWindow.setContent(contentString);
infoWindow.open(map, marker);
});
</text>
}
</script>
For each item in the model you are creating a marker. However the code to add the event listener to the marker (for the click ) is outside of the loop.
The event listener is only being added to the last marker, not every marker.

Resources