Skip to Content

Hi Readers!

First of all Happy New Year to all.This is another interesting topic,

Problem Statement

1. Sometimes we had implemented google map in our application but after searching any address manually, we are unable to show the location on the google map.

2. If we have Latitude & Longitude we are unable to fetch the Address of that particular Lat & Log.

This blog will help you on that.

Solution

Here I will provide solution one by one.

1. Fetching Location based on Address

If we use any search box at the top of our google map, there we can write any address, we need to display the location on my google map with a marker.

Here I am writing the code, after entering the address when you click on the search button.

 onSearch: function () {  
	        var map = this.map;  
	        var address = this.getView().byId("SearchInput").getValue();  
	        var geocoder = new google.maps.Geocoder();
	        this.geocoder.geocode({ 'address': address }, function (results, status) {  
	            if (status == google.maps.GeocoderStatus.OK) {  
	                map.setCenter(results[0].geometry.location);  
	                var marker = new google.maps.Marker({  
	                    map: map,  
	                    position: results[0].geometry.location  
	                });  
	            } else {  
	                sap.m.MessageToast.show("Geocode was not successful for the following reason:"+ status);
	            }  
	        }); 
	        

You will able to have one marker on the entered address on google map.

2. Address Fetching from Lat & Log

Now, sometimes we have Latitude & Longitude but need to have the address. Below code will help on this

var lat= " " ;
var log= " " ;
var latLng = new google.maps.LatLng(lat,log);
	                geocoder.geocode({       
	                        latLng: latLng     
	                        }, 
	                        function(responses) 
	                        {     
	                           if (responses && responses.length > 0) 
	                           {                                	
		                        	var formatedAddress=responses[0].formatted_address;
		                                sap.m.MessageToast.show("You have selected : " + formatedAddress,);
	                           } 
	                           else 
	                           {       
	                             sap.m.MessageToast.show('Not getting Any address for given latitude and longitude.');     
	                           }   
	                        }

Thank you for reading this blog.

Regards,
Rahul Patra
SAP-Fiori Consultant.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. umesh kadlag

    Dear Rahul,

     

    I want latitude and longitude as per state wise.

    i have to plot the map and every state i want to show the colour and status bar.

     

    Regards,

    Umesh k

    (0) 

Leave a Reply