Skip to main content

Sitecore custom field type to convert address to Latitude and Longitude using Google map API

Sitecore custom field type to convert address to Latitude and Longitude using Google map API


Sitecore allows the user to create a custom field type as per requirement. This blog post will explain to you how to create custom field type to convert address to Latitude and Longitude, this is a super easy way to get Latitude and Longitude for Content Editor.

Create new field

  • Switch to Core Database and Navigate to this location “/sitecore/system/Field types”
  • Create new folder “User Defined” using  this “/sitecore/templates/Common/Folder“ template

sitecore, fieldtype, field, googleapi, latitude, longitude, address,
  • Create new item “LatLong” using this template“/sitecore/templates/System/Templates/Template field type”

sitecore, fieldtype, field, googleapi, latitude, longitude, address,
  • Inside “LatLong” item add control name “customfields:LatLongField”
sitecore, fieldtype, field, googleapi, latitude, longitude, address,
  • Create new folder “Menu “ using  this “/sitecore/templates/Common/Folder“ template

sitecore, fieldtype, field, googleapi, latitude, longitude, address,

  • Inside “Set LatLLong”, set below values:
    • Display name: Set Latitude Longitude
    • Message: LatLongField:Set
sitecore, fieldtype, field, googleapi, latitude, longitude, address,
  • Inside “Menu” folder creates “Set LatLLong” item using this template “/sitecore/templates/System/Menus/Menu item” and create similar item with name “Clear”

sitecore, fieldtype, field, googleapi, latitude, longitude, address,

  • Inside “Clear”, set below values:
    • Display name: Clear
    • Message: LatLongField:Clear

sitecore, fieldtype, field, googleapi, latitude, longitude, address,

Implement class for Lat Long field

Create new class “LatLongField” and paste below code


 using Sitecore.Web;  
 using Sitecore.Web.UI.Sheer;  
 using Sitecore.Web.UI.HtmlControls;  
 using Sitecore.Foundation.SOAServices.Services;  
 namespace Example.CustomFields.Fields  
 {  
   public class LatLongField : Edit  
   {  
     public void SetValue(string value) { Value = value; }  
     public override void HandleMessage(Message message)  
     {  
       base.HandleMessage(message);  
       switch (message.Name)  
       {  
         case "LatLongField:Set":  
           Sitecore.Context.ClientPage.Start(this, "GetLatLong");  
           break;  
         case "LatLongField:Clear":  
           Sitecore.Context.ClientPage.Start(this, "ClearLatLong");  
           break;  
       }  
       if (Value.Length > 0)  
       {  
         SetModified();  
       }  
     }  
     public void ClearLatLong(ClientPipelineArgs args)  
     {  
       Value = string.Empty;  
       SetModified();  
     }  
     public void GetLatLong(ClientPipelineArgs args)  
     {  
       string currentvalue = WebUtil.GetFormValue(ID);  
       if (!string.IsNullOrEmpty(currentvalue))  
       {  
         string latlong = Geocoding.GetLocation(currentvalue);  
         if (!string.IsNullOrEmpty(latlong))  
         {  
           SetValue(latlong);  
         }  
         else  
         {  
          SheerResponse.Alert("Please enter correct address");  
         }  
       }  
       else  
       {  
         SheerResponse.Alert("Please enter correct address");  
       }  
     }  
   }  
 }  

Create config file for field type

Create custom config file “Example.CustomFields.config” and add below code

 <?xml version="1.0" encoding="utf-8" ?>  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">  
  <sitecore>  
   <controlSources>  
    <source mode="on" namespace="Example.CustomFields.Fields" assembly=" Example.CustomFields" prefix="customfields" />  
   </controlSources>  
  </sitecore>  
 </configuration>  

Use custom LatLong field in template

Now LatLong field is ready to use. Create field in template and select LatLong field type.

sitecore, fieldtype, field, googleapi, latitude, longitude, address,

Now enter address in “LatLong” field and click on “Set Latitude Longitude”, Google API will convert address to Latitude and Longitude and to clear text box, click on “Clear” link





Comments

Popular posts from this blog

Sitecore Certification Sample Questions

Sitecore Certification Sample Questions I have completed the Sitecore 9 certification in 2019 and Sitecore 8.2 Certification in 2018. Let me tell you if you have some knowledge about Sitecore then it will be easier for you to understand questions and clear certification exam. Your main focus should be to understand the questions and answers carefully because it is too confusing There will be two options to give the exam: Install the software in your laptop (Laptop should have mic and webcam) Authorized training Center If you have proper Internet, then you can choose option 1 otherwise option 2 is a good choice. Disclaimer : Please note that the below question is only for information purpose only. Below question does not claim the accuracy of the content. Sample Questions: Question 1: What is the name of Component architecture guideline of Sitecore? Answer : Helix Question 2 : What is horizontal scaling and vertical scaling? Answer :  https://doc.

Sitecore 9 Forms: Google reCaptcha field

Sitecore 9 Forms: Google reCaptcha field  Re-Captcha is the most important part of any form’s submission. Google reCaptcha run internet bot detector and determined whether a user is a bot or not.  Sitecore Forms does not provide Google reCaptcha field, which was available in WFFM before, so I have created my custom Google reCaptcha.   Below you can find step by step process to create Google reCaptcha field. Create patch config for reCaptcha Sitekey and SecretKey <?xml version="1.0"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <settings> <setting name="ReCaptchaSiteKey" value="site-key" /> <setting name="ReCaptchaSecretKey" value="secret-key" /> </settings> </sitecore> </configuration> Create new viewmodel class RecaptchaViewMode

Create Sitecore Custom Admin Page

Create Sitecore Custom Admin Page Sitecore provides you lots of Admin pages just like ShowConfig.aspx, cache.aspx, Logs.aspx, howServicesConfig.aspx, etc. which you can check from here http://<yoursite>/sitecore/admin/. All Sitecore Admin pages is available in this folder “sitecore/admin/*” Sometimes there can be requirement that you want to create custom Admin page which will be accessible to Admin/Non-Admin user. Here is an example: Create aspx page “CustomAdminPage.aspx” inside “<root>/sitecore/admin” folder <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomAdminPage.aspx.cs" Inherits="Namespace.CustomAdminPage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Custom Admin Page</title> <link rel="stylesheet" type="text/css" href="/sitecore/she