VectorSDK Map FitBounds
by mapmyindia_map
HTML
<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://apis.mapmyindia.com/advancedmaps/api/Token or Key/map_sdk?layer=vector&v=2.0&callback=initMap1" defer async></script>
<div id="main">
<section id="left-menu">
<center><h4>Map FitBounds</h4></center>
<div id="flip1">
<div id="panel1">
<span class="texlable">Position</span>
<textarea name="label" id="position" rows="10" class="form-control">
[[76.324462890625,27.024695711685304],[77.2158203125,28.97189158],[77.2258203125,28.27389158]]
</textarea>
<span class="texlable">CType=0 (default(lat,lng)) <br> CType=1 (set(lng,lat))</span>
<select name="ctype" id="ctype" class="form-control b-shadow">
<option disabled>Select</option>
<option value="0" selected>0</option>
<option value="1">1</option>
</select>
<h4>Options</h4>
<span class="texlable">Padding</span>
<input type="text" id="padding" value="120" placeholder="Enter Fillcolor" class="form-control b-shadow">
<span class="texlable">Duration</span>
<input type="text" id="duration" value="1000" placeholder="Enter Fillcolor" class="form-control b-shadow">
<div id="ch15" style="width:30%; height:40px; display: inline-block; margin-top: 2%;">
<label class="switch">
<input id="checkbox" type="checkbox" Unchecked>
<span class="slider round"></span>
</label>
</div>
</div>
</div>
<!-- <hr> -->
</section>
<section id="right-menu"></section>
</div>
CSS
#main{
width: 100%;
height: 100%;
background-color: gray;
}
#left-menu{
width: 230px;
height: 100%;
background-color: #e0e0e0;
float: left;
overflow: auto;
}
#right-menu{
width: calc(100% - 230px);
height: 100vh;
float: left;
position: fixed;
right: 0;
}
/* Switch checkbox */
.switch {
position: relative;
display: inline-block;
width: 55px;
height: 28px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: 0.4s;
transition: 0.4s;
}
.slider::before {
position: absolute;
content: "";
height: 20px;
width: 20px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: 0.4s;
transition: 0.4s;
}
input: checked+.slider {
background-color: #2196F3;
}
input: focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input: checked+.slider ::before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round::before {
border-radius: 50%;
}
@media only screen and (max-width: 600px) {
#left-menu {
-webkit-transition: height 0.2s;
transition: height 0.2s;
width: 100%;
height: 30%;
background-color: #e0e0e0;
position: absolute;
bottom: 0%;
z-index: 2;
overflow: auto;
}
#right-menu{
width: 100%;
height: 70%;
position: absolute;
z-index: 1;
}
#l1,#cl1{
display: none;
}
}
#flip1
{
color: #3c3c3c;
font-size: 20px;
background-color: #f3f3f3;
padding: 6px 0px;
margin-bottom: 3px;
margin-left: 6px;
margin-right: 6px;
border-radius: 6px;
}
#panel1{
padding: 5px 10px 0px;
display: block;
}
::-webkit-scrollbar {
width: 5px;
display: none;
}
.form-control.b-shadow {
-moz-box-shadow: inset 0 0 5px lightgray;
-webkit-box-shadow: inset 0 0 5px lightgray;
...
JavaScript
// map fitbounds
document.getElementById('checkbox').addEventListener('change', (event) => {
if (event.target.checked) {
map_fitbound();
}
else{
map.setZoom(4);
}
});
// Map Function
var map;
function initMap1() {
map = new MapmyIndia.Map('right-menu', {
center: [28.61, 77.23],
zoomControl: true,
location: true,
});
marker_object = new MapmyIndia.Marker({
map: map,
position: {"lat": "28.61","lng":"77.23"},
draggable: true,
});
}
// fitBounds function
function map_fitbound(){
var position= document.getElementById('position').value;
var ctype = document.getElementById('ctype').value;
var padding= document.getElementById('padding').value;
var duration= document.getElementById('duration').value;
fitBounds = new MapmyIndia.fitBounds({
map:map,
cType:JSON.parse(ctype),
bounds:eval(position),
options:{
padding: JSON.parse(padding),
duration:JSON.parse(duration)
}
});
}
initMap1();