I have the same question as asked here but provided no accepted answer has been given and that my procedure is different, I'm opening a new one.
I have a map inside a Foundation tab that is hidden at the beginning and it only displays part of the map when opened. I've read the issues related to that and tried the attempts there but no luck. I know I have to re-initialize the map on tab change but I can't make it work.
I have a function that holds all the map info:
function startMap(obj){
var markers = new Array();
var glat_center = $(obj).attr('data-glat');
var glong_center = $(obj).attr('data-glong');
var Latlng_center = new google.maps.LatLng(glat_center, glong_center);
var mapOptions = {
zoom: zoom,
scrollwheel: false,
center: Latlng_center,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: mapStyles,
mapTypeControl: false,
streetViewControl: false,
minZoom: 4,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.TOP_RIGHT
}
};
var map = new google.maps.Map(obj, mapOptions);
if($(obj).attr('data-glat') && $(obj).attr('data-glong')){
var glat = $(obj).attr('data-glat');
var glong = $(obj).attr('data-glong');
var Latlng = new google.maps.LatLng(glat, glong);
var marker = new google.maps.Marker({
position: Latlng,
map: map
});
}
}
My html markup looks like this
<div class="tabs" data-section="auto">
<!--two more tabs here-->
<section class="property-location">
<a href="#" class="title" data-section-title>Title</a>
<div class="tab-content" data-slug="section3" data-section-content>
<div class="google-map" data-glat="123456" data-glong="123456" style="width: 100%; height: 440px;">
</div>
</div>
</section>
</div>
So then, what I'm trying to do is call this function when the tab is clicked, as I can't find any other tab events in Foundation docs. First I find the .title of the tab that contains the map, then look if a map exists (just in case) and then I call the function on the div holding the map.
$('.tabs .property-location').find('.title').click(function() {
if(($('.tab-content').find('.google-map')).length) {
//console.log('I found a map');
startMap($('.google-map'));
}
});
I know it's finding the map because I console.log it, but the console throws this error
NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindow.getComputedStyle]
which is on the following src http://maps.gstatic.com/intl/es_ALL/mapfiles/api-3/14/7/main.js
So it's the Google Maps JS that's throwing this error but I can't understand why.
I know it's difficult, but any ideas?
You pass a jQuery-element, but google maps needs a HTMLElement. Use
var map = document.querySelector('.google-map');
startMap(map);
instead of
startMap($('.google-map'));
Instead of querySelector you can use getElementsByClassName
var map = document.getElementsByClassName('google-map')[0];
But the always recommended approach is to refer to the map by an id :
<div id="the-google-map" class="google-map" data-glat="123456" data-glong="123456" style="width: 100%; height: 440px;"></div>
var map = document.getElementById('the-google-map');
Related
I have created a map in Meteor using Leaflet JS. The problem is, I could only get map.panTo to work inside the Template.dynamicmap.rendered area. However, this makes it so anywhere you click on the map pans to the location.
This is the complete rendered area with id and access token removed.
Template.dynamicmap.rendered = function() {
var southWest = L.latLng(35.02035919622158, -121.21049926757814);
var northEast = L.latLng(42.4426214924114, -110.79740478515624);
var mybounds = L.latLngBounds(southWest, northEast);
var map = L.map('map_container',{zoomControl: false, maxBounds: [[37.00035919622158, -119.23049926757814],[40.4626214924114, -112.77740478515624]],}).setView([38.685509760012, -115.86181640625001], 10);
var w = window.innerWidth;
var h = window.innerHeight;
$('#map_container').css({width: w+'px', height: h+'px'});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
bounds: mybounds,
maxZoom: 10,
minZoom: 9,
}).addTo(map);
L.Icon.Default.imagePath = 'packages/leaflet/images';
var marker = L.marker([38.685509760012, -115.86181640625001]).addTo(map);
map.fitBounds(bounds);
};
So I tried putting it in an Template event shown below, this button is not within the map area but still in the dynamicmap template. It does not work though.
Template.dynamicmap.events({
'click input.goto3':(function() {
map.panTo(L.latLng(38.685509760012, -115.86181640625001));
})
});
I receive the error:
"Uncaught ReferenceError: map is not defined"
in the console. Which I have been trying to wrap my head around but no luck. I was hoping someone here could point me in the right direction.
Here is my HTML Template.
<template name="dynamicmap">
<div class="containerbox">
<div class="map_container" id="map_container">
</div>
</div>
<input type="button" class="goto3" value="Go"/>
</template>
You need to make map a global variable:
var map = null;
Template.dynamicmap.rendered = function() {
var southWest = L.latLng(35.02035919622158, -121.21049926757814);
var northEast = L.latLng(42.4426214924114, -110.79740478515624);
var mybounds = L.latLngBounds(southWest, northEast);
map = L.map('map_container',{zoomControl: false, ...
...
I'm trying to add the google map with a marker into my form in order to help users choose the location, but it went wrong somewhere.
As you see, the map didn't display correctly in its area. I've put a marker into the map and also set center there, but the location of that marker is not correct, it hides outside the initial map (in the top right of the map above, instead of the center).
When I hit the F12 button to enable Firebug (in firefox), the map display correctly
Here's the code for creating the form and adding google map:
The form in index.html file:
<div id="map_company" style="float: left; width: 500px;height: 350px;">
<label style="margin-bottom: 5px;">Chọn vị trí hãng xe trên bản đồ:</label>
<div id="map_canvas" style="width: 100%; height: 100%; position: relative;"></div>
</div>
And the javascript in the script file:
//add map to company tab
//Set Center
var myOptions =
{
center: new google.maps.LatLng(21.044813, 105.79864),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
}
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var comMarker = new google.maps.Marker({
position: new google.maps.LatLng(21.044813, 105.79864),
map: map,
draggable: true
});
google.maps.event.addListener(comMarker, 'dragend', function {
document.getElementById("cLat").value = this.getPosition().lat();
document.getElementById("cLon").value = this.getPosition().lng();
});
Can anyone help me to figure out the problem here, I even don't know how to explain it to google.
It appears that you use tabs.
When yes, you must trigger the resize-event of the map when the tab with the map has been selected.
Give your map_canvas a fixed size (in pixels, not percentage) and remove the position = relative. If you use percentages the parent node must also have a size, otherwise it make no sense, (100% of what?).
<div id="map_canvas" style="width: 400px; height: 300px;"></div>
I'm trying to create a View for Google Maps, and actually got it to work according to some examples I found.
the problem is that the map draws correctly only the first time the page is displayed,
if routes are changed and then a map is being drawn again it looks "distorted".
the examples I found are "one page" part of apps.
View:
App.LocationView = Ember.View.extend({
templateName: 'location',
MapView: Ember.View.extend({
map: null,
latitudeBinding: 'controller.content.geometry.lat',
longitudeBinding: 'controller.content.geometry.lng',
didInsertElement: function() {
var mapOptions = {
center: new google.maps.LatLng(0, 0),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(this.$().get(0),mapOptions);
this.set('map',map); //save for future updations
this.$().css({ width: "550px", height: "400px" });
},
reRenderMap : function(){
var newLoc = new google.maps.LatLng(this.get('latitude'), this.get('longitude'));
this.get('map').setCenter(newLoc);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(this.get('latitude'), this.get('longitude')),
map: this.get('map')
});
}.observes('latitude','longitude')
})
});
I show the map with {{view view.MapView}} inside a div#map-holder, in my 'location' template.
I also apply this CSS to the div to "fix" Bootstrap messing with the map controls:
#map-holder img {
max-width: none;
}
how can I fix this ?
EDIT: jsfiddle - http://jsfiddle.net/bsphere/jYfg3/
go to 'settings' and then back to 'map' to see the distorted map
It seems like this is not exactly related to Ember, but to the map redrawing and resizing. With your fiddle, if you resize the browser window, you can see the map displays correctly.
When I saw it, I tried to put the this.$().css({ width: "550px", height: "400px" }); before the contrusction of the map, and it seems to work.
I am using a google maps api v3 to load some data from a kml file. I wish to style the description data when it is shown in an info window to suit my web page.
Now I am trying to set the style:
style="margin-left:-20px;border:2px dotted #897823; et-a;"
...inside the description tag of a Kml Placemark but it is not rendering it properly.
I can see that firebug just shows up the positives values of margin and padding. It entirely ignores the negative margin values. So, I was wondering, are any limitation in using css style attributes for kml files?
<placemark>
<name><![CDATA[First Office Address]]></name>
<description>
<![CDATA[
First Line Information<br>
California addresss<br>
Peak valley<br>
<div class="cInfo">Telephone<br>
Office 9089YUHJT General: (2457TYGFR</div>
]]>
</description>
<Point>
<coordinates>-420.2300,137.5332200,0</coordinates>
</Point>
</placemark>
The issue you are having is due to Content scrubbing in the Api. Scrubbing the contents is a security measure to prevent malicious code from executing.
When a feature balloon's description contents are scrubbed, the following code is removed:
JavaScript
CSS
<iframe> tags
<embed> tags
<object> tags
If you take a look at the markup in a debugger you will see that you are actually getting something like the following:
<div style="font-family: Arial,sans-serif; font-size: small;">
<div id="iw_kml">
First Line Information<br>
California addresss<br>
Peak valley<br>
<div>Telephone<br>Office 9089YUHJT General: (2457TYGFR</div>
</div>
</div>
You don't say how you are opening the info windows, but something like the following should work for you. Basically you suppress the default info window and then build your own custom one.
function initialize() {
var myLatlng = new google.maps.LatLng(0, 0);
var myOptions = {
zoom: 12,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(
document.getElementById("map_canvas"),
myOptions
);
var layer = new google.maps.KmlLayer(
'http://www.yourserver.com/some.kml', {
// prevent default behaviour
suppressInfoWindows: true,
map: map
}
);
// bind the event handler
google.maps.event.addListener(layer, 'click', function(kmlEvent) {
showInfoWindow(kmlEvent.featureData.description);
});
// show a custom info window
function showInfoWindow(text) {
// build your window using whatever, styles, embeds or scripts
// you like. Anything included here will bypass content scrubbing
var content = "<div style='margin-left:-20px;border:2px dotted #897823;'>" + text + "</div>";
var infowindow = new google.maps.InfoWindow({
content: content
})
}
}
Obviously you can replace style='...' with class='whatever' which would then allow you to define the CSS style in an external file.
I am trying to change the KML placemark icons of a KML overlay in a sample Maps application that I am working on.
Here's the sample code -
function seekml() {
var myLatlng = new google.maps.LatLng(40.65, -73.95);
var myOptions = {
zoom: 14,
mapTypeControl: true,
center: myLatlng,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
position: google.maps.ControlPosition.TOP_RIGHT
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
url_end = "?nocache=" + (new Date()).valueOf(); //For No KML Caching
myKML = "http://kmlscribe.googlepages.com/SamplesInMaps.kml" + url_end
gMap = new google.maps.Map(document.getElementById("map"), myOptions);
var ctaLayer = new google.maps.KmlLayer(myKML,{suppressInfoWindows: true});
ctaLayer.setMap(gMap);
google.maps.event.addListener(ctaLayer, 'click', function(event) {
this.setIcon(gYellowIcon);
});
}
gYellowIcon has been defined in my code before -
var gYellowIcon = new google.maps.MarkerImage(
"image url",
new google.maps.Size(31, 31),
new google.maps.Point(0, 0),
new google.maps.Point(6, 20));
I want to change the KML overlay placemarks, when the user clicks on any of the placemarks shown on the KML overlay. The above code doesn't work.
I'm currently working on the exact same thing and, in my case, I could directly edit the KML file. If you have access to it and can edit it, here's what I did:
1) Right under <document> tag, paste something like this:
<Style id="desired_id">
<IconStyle>
<Icon>
<href>http://www.yourwebsite.com/your_preferred_icon.png</href>
<scale>1.0</scale>
</Icon>
</IconStyle>
</Style>
The scale parameter is not supported in Google Maps at the moment. Here you can check all supported elements of KML in Google Maps:
http://code.google.com/intl/en-EN/apis/kml/documentation/kmlelementsinmaps.html
And here you've got some info about compatibility between KML and GMaps:
http://code.google.com/intl/en-EN/apis/kml/documentation/mapsSupport.html
2) Once you've defined you're style, you can refer to it on each Placemark item by adding the following to it:
<styleUrl>#desired_id</styleUrl>
Now, all your placemarks should display showing your custom icon.
Hope it helps.
EDIT: Sorry I didn't see the on click part. This isn't quite what you need then. I'll leave it in case it helps someone else. Sorry for that.