Styling kml with css in google maps v3? - css

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.

Related

mapbox logo spread out of grid in Wordpress

I am using Mapbox GL JS, embedding a map into Wordpress, via plug-in Page Builder-Custom HTML.
HTML
<div id='map'></div>
<script>
mapboxgl.accessToken = '*******';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v9', // stylesheet location
center: [144.9509223,-37.8253897], // starting position [lng, lat]
zoom: 9 // starting zoom
});
</script>
CSS
#map { position:static; }
However, the logo always goes out of the grid. Mapbox and 'i' button actually spread full page width. I reckon there is a mapbox css I need to deeply reach to.
I can't use mapbox plugin, as the map will have javascript for user interaction. Any suggestions, please? Thanks.
changed to Google Maps, now works like a charm...

Can I use a Google static map as a background?

I'm trying to apply a Google static map as a background for a div element. Is this possible? I'm using the image-background CSS property at the moment.
You can simply save the image by navigating to the URL of your static map and use it as a background like any other image.
.map-div {
background-image:url('static-map.png');
}
The maximum resolution it can be is 640x640, which may or may not be sufficient, however you can use a scale value of 2 to get a 1280x1280 image.
Yes, this is very possible.
You'll need a Google API key and some knowledge of CSS3.
Read more about how to do it and get some sample code here: http://blog.wadehammes.com/post/3837158298
Use this code to disable all controls:
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(-33, 151),
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
}

Setting a KML layer transparency

Is there a way to set the transparency of a kml layer when you add it? I am adding a kml layer that you cannot see through to the streets below. Is there a way to accomplish this
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var ctaLayer = new google.maps.KmlLayer({
url: 'images/test3.kml'
});
var ctaLayer2 = new google.maps.KmlLayer({
url: 'images/test.kml'
});
var ctaLayer3 = new google.maps.KmlLayer({
url: 'images/kmztest2.kmz'
});
ctaLayer3.setMap(map);
ctaLayer2.setMap(map);
ctaLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
As KML are added by Google Maps API as images in the DOM, you can change its opacity with CSS by searching the <img> elements that contain "kml" in its src attribute:
#map img[src*='kml'] {
opacity: .5;
}
Also, you can achieve this using jQuery:
jQuery("#map").find("img[src*='kml']").css("opacity","0.5");
But keep in mind that when user zoom's out or moves the map, new KML images will be loaded, so you'll have to call that jQuery function again.
You can't modify the opacity of the polygons in a KmlLayer. You have three options (that I can think of):
define the opacity in the KML
example, the KML looks like this:
<Style
id="Style1">
<LineStyle><color>66000001</color><width>1</width></LineStyle>
<PolyStyle>
<color>00ff6633</color> <!-- first 00 is opacity of zero -->
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<Placemark id="Opacity0">
<name>Opacity 0</name>
<visibility>1</visibility><open>0</open><styleUrl>#Style1</styleUrl>
<Polygon><extrude>0</extrude><tessellate>1</tessellate><altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs><LinearRing><coordinates>-116.365673309192,43.6628960911185 -116.365591334179,43.6560111958534 -116.364375539124,43.6559975333512 -116.364402864128,43.6483204644173 -116.359539767727,43.6483955662698 -116.359567092732,43.64422573708 -116.356452545151,43.6442223004997 -116.356329582632,43.6403188481927 -116.355482675135,43.6384234484285 -116.354444492608,43.6376550793648 -116.354198567569,43.6375697515905 -116.354198567569,43.6375560890883 -116.354348855093,43.6375355534256 -116.352818906307,43.6375834140927 -116.349636046216,43.6375697515905 -116.349677033722,43.6339155770838 -116.317438473925,43.6339155770838 -116.314392238855,43.6339600011706 -116.314187301323,43.6484194546938 -116.334391040727,43.6484843306243 -116.33440470323,43.6627594660968 -116.335292598233,43.6629438679665 -116.336767980829,43.6629097536206 -116.359348576516,43.6629985179752 -116.360673587769,43.6628994438797 116.365673309192,43.6628960911185</coordinates>
</LinearRing></outerBoundaryIs></Polygon>
</Placemark>
import the KML into FusionTables, and use a FusionTablesLayer (which allows you to change the opacity of polygons) (No longer useful as FusionTables will be turned down/off on December 3, 2019)
If the KML is not too complex, use a third party parser (geoxml3 or geoxml-v3, which will parse the KML and render it as native Google Maps Javascript API v3 objects (which allow you to modify the opacity).
This solution is not perfect because it causes a slight flash when changing zoom levels, but some may find this useful:
google.maps.event.addListener(map, 'tilesloaded', function() {
$("#map").find("img").css("opacity","0.5");
});
If you use preserveViewport: true, you shouldn't have any issues.
var kmlLayer = new google.maps.KmlLayer(kmlsrc, {
suppressInfoWindows: true,
preserveViewport: true,
map: map
});

google maps v3 change size of infowindow

I would like to set the size of an infowindow to fit the content inside. The standard infowindow is too wide I tried to use maxWidth but it doesn't seem to work. What is the best way to resize an infowindow?
see code:
window.setContent( inspStates[i].name+ "<br/>"+"total: "+inspStates[i].totalInsp+ "<br/>"+ info);
the information that will be displayed in the bubble looks like this( \n means next line)
NY \ntotal 60 \n2009: 10 \n2010: 20 \n2011: 30
What you want to do is replace the "standard" Googlemaps infoWindow with your own, and put your content into it. Once you replace the standard GM infoWindow, you have a lot of latitude to work with. What I did is this:
Add a new style ID called #infoWindow, and set its width.
#infoWindow {
width: 150px;
}
Outside of any functions within my Javascript, I create a new infoWindow:
var infoWindow = new google.maps.InfoWindow();
Create a new var within the function that sets the information displayed by the marker(s), and set its value to the content:
var html = '<div id="infoWindow">';
html += 'NY<br />total 60<br />2009: 10<br />2010: 20<br />2011: 30';
html +='</div>';
Call the createMarker function using the location information you would have created elsewhere, and include the html var as one of the arguments:
var marker = createMarker(point,name,html);
The createMarker function you would declare elsewhere, which would bring all of the information together, display the marker on your map, and display the information above when it's clicked on:
function createMarker(latlng,name,html) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: name
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map,marker);
});
}
Just so everyone is clear, if they stumble on this thread.
All you need to do is set the height and width of a container in the your info window content.
You don't need to override google classes and you don't need to get overly complicated about this.
var contentString = '<div class="map-info-window">'+
'<h1>My Info Window</h1>'+
'<p>Hello World</p>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString, });
In my css I have this:
.map-info-window {
width:200px;
height:200px;
}
That's it. That's all you need to do to set the width of a Google Maps InfoWindow.
For me this CSS works fine:
.gm-style-iw {
width: 324px !important;
height: 120px !important;
}
please take a look at following API references and examples:
Google Map V3 Examples:
https://developers.google.com/maps/documentation/javascript/examples/
Google Map V3 API Ref:
https://developers.google.com/maps/documentation/javascript/reference#MapOptions
To set the size of the bubble info-window inside a google map it's enough to add the following css in your CSS file:
.gmap-popup {
max-width:204px;
max-height:110px;
}
Adapted from this discussion.
JFrancis answers is the right one, just don't forget to add "!important" in your CSS when you set the width! This might look like a minor thing, but if you don't do it, your CSS will simply get overwritten!
#infoWindow {
width: 150px !important;
}

Changing KML Placemark icons on click in Google Maps API V3

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.

Resources