Share current location and print currebt location

Share current location and print currebt location using google maps

by Abhay Singh

HTML

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBtGHvvrBUZ1WgcTvsR4v-K5pX6z37wRIE&amp;callback=initMap"></script>
<div id="result"> 

</div>
<div class="xyz">
Click Me
</div>

JavaScript

$('.xyz').click(function() {
if(navigator.geolocation) {	
	navigator.geolocation.getCurrentPosition(function(pos) {
		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
		geocoder.geocode({'latLng': latlng}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				//Check result 0
				var result = results[0];
				//look for locality tag and administrative_area_level_1
				var city = "";
				var state = "";
				for(var i=0, len=result.address_components.length; i<len; i++) {
					var ac = result.address_components[i];
					if(ac.types.indexOf("locality") >= 0) city = ac.long_name;
					if(ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
				}
				//only report if we got Good Stuff
				if(city != '' && state != '') {
					$("#result").html("Hello to you out there in "+results[0].formatted_address);
				}
			} 
		});
	
	});
}
});