Get nearest place location using latitude and longitude

Using latitude and longitude get nearest place location name like school, hospital, restaurant, atm, bank, airport.
Here link is supported places provided by google map.

Place Types | Google Places API | Google Developers

Step 1:


Include the js file.
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

-----------------------------------------

Step 2:


Add the below script.

<script type="text/javascript">
        var map;        
        var pyrmont = new google.maps.LatLng(22.299695, 70.799324 );
        function initialize() {
            map = new google.maps.Map(document.getElementById('mapdetail'), {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: pyrmont,
            });
            search_place();
        }
     
        function search_place(){
            if(!latLng){
                var latLng = pyrmont;
            }           
            var type = 'school';
            var request = {
                location: latLng,
                radius: 10000,
                types: [type]
            };
            var service = new google.maps.places.PlacesService(map);
           
            service.search(request, function(results, status) {
                if (status == google.maps.places.PlacesServiceStatus.OK) {
                    for (var i = 0; i < results.length; i++) {                       
                            document.getElementById('school').innerHTML +=results[i].name+', ';
                    }
                }
            });
         }
        google.maps.event.addDomListener(window, 'load', initialize);
</script>

-----------------------------------------

Step 3:


Add the div id for place where to view place name like

<div id="mapdetail"></div>
<div id="school"></div>

Comments

Popular posts from this blog

Bike/Scooter Transport via Indian Railway Procedure

Upgrade php version in xampp

Fraud buyer on OLX & Quicker, Please be safe before deal