I've searched a lot about creating a custom control for Google Maps Api v3, and I found others use it as Google documentation. They create the divs and style it using JS, which I think is not a good practice. I think this violates the separation of concerns design principle I mean, writing CSS code inside JS or HTML.
Apart from the best practise question, I've tried their code examples from the above link, but it doesn't work it raised the following error:
( **Uncaught TypeError: Cannot read property 'zIndex' of undefined** ) ..
Here is the code for putting [Zoom] button.
HTML file
<div id="control-div" class="control-div">
<div id= "control-ui" class="control-ui" title = "Click to set the map to Home">
<div id="control-text" class="control-text">
<p> Zoom </p>
</div>
</div>
</div>
CSS file
.control-ui{
background-color: white;
border-style: solid;
border-width: 2px;
cursor: pointer;
text-align: center;
width: 90px;
height: 30px;
}
.control-text{
front-family: Arial,sans-serif;
font-size: 12px;
padding: 4px 4px;
}
JS file (Google map initializer)
var controlDiv =$("#control-div");
var controlUI = $("#control-ui");
var controlText = $("#control-text");
controlUI.click(function() {
map.setZoom(11);
});
controlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.
Google Maps - Turn On All Controls Some controls appear on the map by default; while others will not appear unless you set them. Adding or removing controls from the map is specified in the Map options object.
Clear app data Google Maps stores data like shared locations, saved locations, and map tiles on your phone or tablet. Clearing this data will do the following things: Delete cache (including search suggestions, direction searches, map tiles, and activity page content stored on your device) Delete cookies.
A control is expected to be a DOMNode, but you supply a jQuery-object.
This should work:
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With