Forum Discussion

Manvendra's avatar
Manvendra
Binge Watcher
5 years ago

Is there any way to find zip code through user location on roku?

I need to build a feature in my app, where i need to show some content based on zip-code. Is there any way to find user location and ultimately find zip code?

6 Replies

  • This is very doable but as you and another user said, you need an external api and more importantly, one that is reliable in accuracy.

    I can only recommend one api that has been flawless in which I use for weather features on the Roku and web apps.

    You can see a demo with the response on page load and get a free api at https://ipstack.com/

    Now we add a little bit of. code.

     

    deviceInfo = CreateObject("roDeviceInfo")
    ip = deviceInfo.GetExternalIp()
    url = "http://api.ipstack.com/" + ip + "?access_key=yourIpStacksApiKey"
    
    m.locationData = createObject("roSGNode", "locationDataReader")
    m.locationData.contenturi = url
    m.locationData.observeField("content", "locationDataResponse")
    m.locationData.control = "RUN"
    
    
    

    Create a task

     

     

    <?xml version = "1.0" encoding = "utf-8" ?>
    <component name = "locationDataReader" extends = "Task" >
    
      <interface>
        <field id = "contenturi" type = "uri" />
        <field id = "content" type = "node" />
      </interface>
    
      <script type = "text/brightscript" >
    
        <![CDATA[
    
         sub init()
           m.top.functionName = "getContent"
         end sub
    
    ' **********************************************
    
        sub getContent()
          apiurl = m.top.url
          m.port = CreateObject ("roMessagePort")
          dataRequest = CreateObject("roUrlTransfer")
          dataRequest.setURL(apiurl)
          dataRequest.EnableEncodings(true)
          https = "https:"
          if LCase(Left(apiurl, Len(https))) = https
            dataRequest.SetCertificatesFile ("common:/certs/ca-bundle.crt")
            'dataRequest.AddHeader ("X-Roku-Reserved-Dev-Id", "")
            dataRequest.InitClientCertificates ()
          end if
          response = dataRequest.getToString()
    'the data will returned as json and you can decide to parse or use regex to get the zipcode
    zipRegex = CreateObject ("roRegex", "zip.:.(\d+).,.latitude", "")

    if zipRegex.isMatch(response)
    zipcode = zipRegex.Match(response)
    'if the regex returned data
    print zipcode
    end if
    'print the whole json response
    print response end sub ]]> </script> </component>

     

     

  • I don't know if there is a inbuilt way. But, you can use external API. you can find zip code by Passing a IP Address. This is not a good way. Currently, Roku not Blocking any External API may be in Future like IOS. I hope this is helpful

    • Manvendra's avatar
      Manvendra
      Binge Watcher

      chaklasiyanikun Thanks for the reply. This is the same i have tried, but results are not very accurate with ip address.

      • chaklasiyanikun's avatar
        chaklasiyanikun
        Roku Guru

        Manvendra It depends on API. I tried many API. and Found There is not a single one that gives an accurate result. But, I suggest one API which is better than all other. I'm not sure this is giving accurate result. But better than other API. I hope this is helpful.