Function to determine latitude and Longitude

Permalink Browser Info Environment
I am feeding some map data in manually through a custom script and this is working great. However my map data has address information, but does NOT have latitude and longitude. In looking at the way you set it up for adding items, it seems that the system looks up the latitude and longitude when you add in the address during the form entry and stores it (smart!).

Before I searched much more I was wondering if there was a code snippet you used that you could point me to that I could reuse to look up lat/long when I am manually inserting data into the table - as you do upon form entry.

Thanks for any help you can provide.

Type: Pre-Sale
Status: Resolved
plschneide
View Replies:
plschneide replied on at Permalink Reply
plschneide
FYI I used a variation of code found here:
https://gist.github.com/madeinnordeste/3250820...

Working well.
mesuva replied on at Permalink Reply
mesuva
Oh great, I was actually just writing you a reply - did you still need some suggestions here, or are you all sorted?

The main point I was going to say is that this addon uses the Javascript API (so you don't need two different keys), whereas if you are doing more of a server based import you'd likely need different code.
mesuva replied on at Permalink Reply
mesuva
That looks like the javascript API anyway now that I look at it - that should be all you need to do then really.

Just be aware of the 50 per second limit, quite easy to hit!
plschneide replied on at Permalink Reply
plschneide
Ah good call I've got about 200 to process - do you know how long I need to pause between sets of 50 or so?
mesuva replied on at Permalink Reply
mesuva
I think only a second

There's also a 2500 per day limit I believe, easy to hit too if you do the import a few times
plschneide replied on at Permalink Reply
plschneide
Any case anyone needs the code:
$geoaddress = $address .", CO, " . $zipcode . ", United Sates"; //Address
      //slow down if needed
      if ($totalcount > 45) {
          sleep(5);
          $totalcount = 0;
      }
        // Get JSON results from this request
        $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($geoaddress).'&sensor=false');
        $geo = json_decode($geo, true); // Convert the JSON to an array
        if (isset($geo['status']) && ($geo['status'] == 'OK')) {
            $latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
            $longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
        }

concrete5 Environment Information

# concrete5 Version
Core Version - 8.3.2
Version Installed - 8.3.2
Database Version - 20180122213656

# concrete5 Packages
Hutman Ads (1.0.1), Location Map (1.0.7), Magic Tabs (7.1.2), Migration Tool (0.5.4), Quick Tabs (1.0), Responsive Embed (1.0.1), Responsive Theme (2.1.9), Zoomer (1.0.1)

# concrete5 Overrides
None

# concrete5 Cache Settings
Block Cache - On
Overrides Cache - On
Full Page Caching - Off
Full Page Cache Lifetime - Every 6 hours (default setting).

# Server Software
Apache/2.4.29 (cPanel) OpenSSL/1.0.2n mod_bwlimited/1.4

# Server API
cgi-fcgi

# PHP Version
5.5.38

# PHP Extensions
bcmath, calendar, cgi-fcgi, Core, ctype, curl, date, dom, ereg, exif, fileinfo, filter, ftp, gd, hash, iconv, imap, json, libxml, mbstring, mcrypt, mhash, mysql, mysqli, mysqlnd, openssl, pcntl, pcre, PDO, pdo_mysql, pdo_sqlite, Phar, posix, pspell, readline, Reflection, session, SimpleXML, sockets, SPL, sqlite3, standard, tokenizer, wddx, xml, xmlreader, xmlwriter, xsl, Zend Guard Loader, Zend OPcache, zip, zlib

# PHP Settings
max_execution_time - 30
log_errors_max_len - 1024
max_file_uploads - 20
max_input_nesting_level - 64
max_input_time - 60
max_input_vars - 1000
memory_limit - 128M
post_max_size - 120M
sql.safe_mode - Off
upload_max_filesize - 115M
mysql.max_links - Unlimited
mysql.max_persistent - Unlimited
mysqli.max_links - Unlimited
mysqli.max_persistent - Unlimited
pcre.backtrack_limit - 1000000
pcre.recursion_limit - 100000
session.cache_limiter - <i>no value</i>
session.gc_maxlifetime - 7200
opcache.max_accelerated_files - 2000
opcache.max_file_size - 0
opcache.max_wasted_percentage - 5
zend_optimizerplus.max_accelerated_files - 2000
zend_optimizerplus.max_file_size - 0
zend_optimizerplus.max_wasted_percentage - 5

Browser User-Agent String

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.