<?php if($mapi=="Google"){?>
<div class="alert-warning">Adresiniz (mahalle, sokak ve numara), Haritadaki adresinizi kontrol ediniz. Doğru değilse haritayı veya işaretçiyi sürükleyerek ayarlayınız</div>
<?php for ($x = 1; $x <= 10; $x++) {?>
<div class="v3-input-group mt10" style="<?php if($x>1){echo "display:none;";}?>" id="d<?php echo $x?>">
	<input type="text" id="l<?php echo $x?>" class="form-control autocomplete loc notranslate" placeholder="Konum girin" autocomplete="off">
	<span class="v3-input-group-btn">
		<button class="v3-btn v3-btn-success" onclick="toggle('#d<?php echo $x+1?>')" style="padding: 4px 8px;" type="button" onclick=""><i class="fa fa-plus" aria-hidden="true"></i></button>
	</span>
</div>
<?php }?>
<?php foreach($jsm as $key=>$value){?>
<div class="v3-input-group mt10" style="" id="dl<?php echo $x?>">
	<input type="text" id="ll<?php echo $x?>" class="form-control autocomplete loc notranslate" placeholder="Konum girin" value="<?php echo $value["adress"]?>" lat="<?php echo $value["lat"]?>" lng="<?php echo $value["lng"]?>" city="<?php echo $value["city"]?>" district="<?php echo $value["district"]?>" autocomplete="off">
	<span class="v3-input-group-btn">
		<button class="v3-btn v3-btn-danger" onclick="Adress_remove('dl<?php echo $x?>')" style="padding: 4px 8px;" type="button" onclick=""><i class="fa fa-minus" aria-hidden="true"></i></button>
	</span>
</div>
<?php }?>
<textarea id="jsresult" name="locations" class="form-control" style="display:none;"><?php Print_r($jsn18)?></textarea>
<div id="map" class="maps" style="height: 85vh; width: 100%;"></div>
<script>
		<?php if($jsm<>""){?>
		jQuery(document).ready(function(){mapLoader();});
		<?php }?>
		jQuery(".loc").keyup(delay(function (e) {
			var id = jQuery(this).attr("id");
			var adres = jQuery(this).val();
			iCoordinates(adres,id);
			setTimeout(function(){ createJSON(); mapLoader() }, 1000);
		}, 1000));
		function Adress_remove(id) {
			jQuery('#'+id+'').remove();
			setTimeout(function(){ createJSON(); mapLoader() }, 1000);
		};
		function iCoordinates(adres,id) {
			jQuery.getJSON('https://maps.google.com/maps/api/geocode/json?address='+adres+'&sensor=false&key=AIzaSyBxhmTCArabnNgXc6IGM7EEpgohO_mECWs',function(data) {
				jQuery('#'+id+'').attr('lat',data.results[0].geometry.location["lat"]);
				jQuery('#'+id+'').attr('lng',data.results[0].geometry.location["lng"]);
				jQuery.getJSON('https://maps.google.com/maps/api/geocode/json?latlng='+data.results[0].geometry.location["lat"]+','+data.results[0].geometry.location["lng"]+'&sensor=false&key=AIzaSyBxhmTCArabnNgXc6IGM7EEpgohO_mECWs',function(data2) {
					for (var i=0; i<data2.results[0].address_components.length; i++) {
						for (var b=0;b<data2.results[0].address_components[i].types.length;b++) {
							if (data2.results[0].address_components[i].types[b] == "administrative_area_level_1") {
								city= data2.results[0].address_components[i];
								break;
							}
							if (data2.results[0].address_components[i].types[b] == "administrative_area_level_2") {
								distinc= data2.results[0].address_components[i];
								break;
							}
						}
					}
					jQuery('#'+id+'').attr('city',city.long_name);
					jQuery('#'+id+'').attr('district',distinc.long_name);
				});
			});
		}
		function createJSON() {
			var jsonObj = [];
			jQuery(".loc").each(function() {
				var id=""; var adres=""; var lat=""; var lng=""; var city=""; var district="";
				var id = jQuery(this).attr("id");
				var adres = jQuery(this).val();
				var lat = jQuery(this).attr('lat');
				var lng = jQuery(this).attr('lng');
				var city = jQuery(this).attr('city');
				var district = jQuery(this).attr('district');
				if(adres!=""){
					item = {};
					item["id"] = id;
					item["adress"] = adres;
					item["lat"] = lat;
					item["lng"] = lng;
					item["city"] = city;
					item["district"] = district;
					jsonObj.push(item);
				}
			});
			var jsresult = JSON.stringify(jsonObj);
			jQuery("#jsresult").val(jsresult);
		}
		function mapLoader() {
			var jsresult = jQuery("#jsresult").val();
			var map = new google.maps.Map(document.getElementById('map'), {
				zoom: 10,
				center: new google.maps.LatLng('41.0817984','29.1666967'),
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			var infowindow = new google.maps.InfoWindow();
			var marker, i;
			var locations = JSON.parse(jsresult);
			jQuery.each(locations, function(i) {
				marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i].lat, locations[i].lng), map: map});
				var point = marker.getPosition();
				marker.setMap (map);
				marker.setDraggable (true);
				google.maps.event.addListener(marker, "dragend", function(event) {
					var point = marker.getPosition();
					jQuery('#'+locations[i].id+'').attr('lat',point.lat().toFixed(5));
					jQuery('#'+locations[i].id+'').attr('lng',point.lng().toFixed(5));
					map.panTo(point);
					createJSON();
				});
				google.maps.event.addListener(marker, 'click', (function(marker, i) {
					return function() {
						infowindow.setContent(locations[i].adress);
						infowindow.open(map, marker);
					}
				})(marker, i));
			});
		};
	</script>
<?php }?>