How to convert zipcode to longitude and latitude? - actionscript-3

I want to find the longitude and latitude of place with a the help to a zipcode.
Can anyone tell me how to do that?
An example in actionscript what be very much helpful for me. Because i am making a project in Flex.
Regards
Zee

Do you have access to a long/lat database? if not, i believe you can use the google maps API to do that lookup.
Oh .. I just noticed Chris' answer. I am not familiar with geonames. You might also want to get familiar with "http://freegeographytools.com/" which has a ton of geocoding, gps, etc. resources for a whole range of projects.
Ahhh ... I just visited Eric's blog post. That is excellent! I will definitely hook up with google for more details in a future project.

If you are looking for on demand geocoding, use Google. They provide a simple pox web service version.
I wrote a blog about this a while back. Check out this link for step by step instructions for using this simple geocoding.
Cheers,
Eric

I got my solve you can also
function getLatLong($code) {
$mapsApiKey = 'ABQIAAAAsV7S85DtCo0H9T4zv19FoRTdT40ApbWAnDYRE0-JyP5I6Ha9-xT9G5hCQO5UtOKSH5M3qhp5OXiWaA';
$query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
$data = file_get_contents($query);
// if data returned
if($data) {
// convert into readable format
$data = json_decode($data);
$long = $data->Placemark[0]->Point->coordinates[0];
$lat = $data->Placemark[0]->Point->coordinates[1];
return array('Latitude'=>$lat, 'Longitude'=>$long);
} else {
return false;
}
}

There are several places to get zip code databases, in several formats. Load it into your favorite RDBMS, and query away.
Alternatively, you can use someone else's web service to look up the value for you. Other answers have posted possible web services; also, geocoder.us appears to have support for ZIP code lookup now as well.

There is now a better solution for this using the most recent Google Maps API (v3). The following is a slightly modified example from multiple sources. I have to give most of the credit to them. It's PHP, using cURL to retrieve the data from Google, but you could also use Ajax.
function address_lookup($string){
$string = str_replace (" ", "+", urlencode($string));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
return null;
}
$geometry = $response['results'][0]['geometry']['location'];
$array = array(
'lat' => $geometry['lat'],
'lng' => $geometry['lng'],
'state' => $response['results'][0]['address_components'][3]['short_name'],
'address' => $response['results'][0]['formatted_address']
);
return $array;
}
$zip= '01742';
$array = address_lookup($zip);
print_r($array);

The easiest way out is using GoogleMap Api. Say you have a zipcode in a varible $zipcode.
$latlongUrl = 'http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:'.$zipcode;
$data = file_get_contents($latlongUrl); // you will get string data
$data = (json_decode($data)); // convert it into object with json_decode
$location = ($data->results[0]->geometry->location); // get location object
$location is the object with Latitude and Longitude value.

Related

Getting JSON data results in warning: htmlspecialchars() expects parameter 1 to be string, array given in wp-includes/formatting.php on line 4529

I paid someone to make a plugin for my website 5 years ago and now I'm trying to get it to work again. I have no knowledge of how JSON works so this is my first experimenting with it. Basically my plugin would use the Iframely API to get a summary of an article and save the data to a custom field from a URL in the editor.
The code I have in question in my function.php is this:
function post_extra_save( $post_id, $post){
global $pagenow;
if ($pagenow == 'post.php') {
if ( has_post_format('link', $post_id)) {
$url = get_post_field('post_content', $post_id);
$request_url = 'https://iframe.ly/api/iframely?url='. urlencode($url) .'&api_key='.get_field_object('api_key', 'option')['value'];
$response = wp_remote_get( esc_url_raw( $request_url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
update_field('field_61942d74195e7', $api_response);
}
}
}
add_action( 'save_post', 'post_extra_save', 10, 2 );
What it does is send a URL plus the API key to Iframely API, returns the JSON and save the raw output to a custom field.
When I save the post, I get this error message:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /wp-includes/formatting.php on line 4529
Any idea what this means?
Found my problem. What I got it to work was it gets the URL from the post content, encode it from the raw JSON and decode it.
$request = wp_remote_get( 'https://iframe.ly/api/iframely?url='. urlencode($url) .'&api_key='.get_field_object('api_key', 'option')['value'] );
$data_raw = json_encode( wp_remote_retrieve_body( $request ));
$data = json_decode($data_raw);

Using Mojolicious: access a perl hash, passed through an ajax call as json, in javascript [duplicate]

Using DBIx::Class, I found a solution to my issue, thankfully. But I'm sure there has to be a nicer way.
my $record = $schema->resultset("food")->create({name=>"bacon"});
How would I turn this record into a simple hashref instead of having to make this call right after.
my record = $schema->resultset("food")->search({name=>"bacon"})->hashref_array();
Ideally I want to be able to write a code snippet as simple as
{record=> $record}
instead of
{record => {name => $record->name, $record->food_id, ...}}
This would drive me insane with a table that has alot more columns.
I assume you're talking about DBIx::Class?
my $record = $schema->resultset("food")->create({name=>"bacon"});
my %record_columns = $record->get_columns;
# or, to get a HashRef directly
my $cols = { $record->get_columns };
# or, as you've asked for
my $foo = { record => { $record->get_columns } };
What you're looking for is included in DBIx::Class as DBIx::Class::ResultClass::HashRefInflator.

Update a WP database table from multiple tables

I am using the WP Google Maps app. This allows map marker data to be entered on the back-end and displayed on maps on the front-end.
I am trying to adapt this so that an Author can create a post, and the data from the post populates the WP Google Maps table so that it appears on the map. In other words, enabling non-admin users to create map markers with additional fields.
The main information for WP Google Maps is stored in a table called wp_wpgmza. I have tried to write an SQL trigger that updates this table with data from wp_posts and wp_postmeta each time a new post is made but I can't crack it. Something about pulling from the two tables and the structure of wp_postmeta
Any suggestions on another way around this problem?
Thanks D
In WordPress, hooks and filters are often used to accomplish most things like that.
You can easily use the following hooks that call your function before saving/inserting a post:
do_action('save_post', $post_ID, $post);
do_action('wp_insert_post', $post_ID, $post);
Here's a sample usage:
function my_function($post_ID, $post) {
}
add_action('save_post', 'my_function', 2);
Inside my_function you could use $wpdb->insert to enter the data to the table.
function my_function($post_ID, $post) {
global $wpdb;
$table = $wpdb->prefix . 'wpgmza';
$data = array(
'column1' => 'value1',
'column2' => 'value2'
);
$wpdb->insert($table , $data);
}
add_action('save_post', 'my_function', 2);
Alternatively, you can use function_exists() and a function used by the plugin itself to insert to the map table.
function my_function($post_ID, $post) {
if (function_exists('some_google_map_app_function')) {
some_google_map_app_function();
}
}
add_action('save_post', 'my_function', 2);
Note: $post_ID, $post are parameters you can use to take a peak into the post data and/or pass along to that google maps function or insert to the table.
Useful Resources:
https://codex.wordpress.org/Function_Reference/add_action
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

How to insert an order into Infusionsoft via API

I have the code to insert the items into to an order as follows,
$ordId = 1;
$prodId = 221;
$type = '';
$price = 1000;
$qty = 5;
$desc = DESC;
$notes = "test notes";
$test = $app->addOrderItem($ordId, $prodId, $type, $price, $qty, $desc, $notes);
Is there any functions/methods available for inserting the orders directly into Infusionsoft?
If you're using the InfusionSoft PHP SDK, you have a couple of options. (1) Use the OrderService API's placeOrder() function, (2) Use the InvoiceService API to create a blank invoice, add line items to that invoice, and charge it. The OrderService API is a good one-off option; but the InvoiceService allows much more flexibility.
Using OrderService.placeOrder
$order = $app->placeOrder(
(int)$contactId,
(int)$creditCardId,
(int)$payPlanId,
(array(int))$productIds,
(array(int))$subscriptionIds,
(bool)$processSpecials,
(array(str))$promoCodes
)
NOTE: The OrderService API requires you to have already added a Contact via the ContactService API, and a Credit Card via the DataService API (by adding it to the CreditCard table).
You can use the order service. Details can be found here http://help.infusionsoft.com/api-docs/orderservice
check the complete code here
$app = new iSDK();
// perform authorization tasks
$carray = array(
$app->key,
$contactId,
$creditCardId,
$payPlanId,
array($productId1, $productId2),
array($subscriptionPlanId1, $subscriptionPlanId2),
$processSpecials,
array($promoCode1, $promoCode2)) // array of strings
);
$app->placeOrder($carray);

Soundcloud API doesn't explicitly support pagination with json

Specific example I was working with:
http://api.soundcloud.com/users/dubstep/tracks.json?client_id=YOUR_CLIENT_ID
You'll get their first 50 tracks, but there is not next-href object like what you see in the xml version.
However, you can use offset and limit and it works as expected- but then I would need to "blindly" crawl through tracks until there are no more tracks, unlike with the XML version which gives you the "next page" of results. I wouldn't have even noticed it was paginated except by chance when I was searching the json object and noticed there was exactly 50 tracks (which is suspiciously even).
Is there a plan to support the next-href tag in json? Am I missing something? is it a bug that it's missing?
There is an undocumented parameter you can use linked_partitioning=1, that will add next_href to the response.
http://api.soundcloud.com/users/dubstep/tracks.json?client_id=YOUR_CLIENT_ID&linked_partitioning=1
for ex :
// build our API URL
$clientid = "Your API Client ID"; // Your API Client ID
$userid = "/ IDuser"; // ID of the user you are fetching the information for
// Grab the contents of the URL
//more php get
$number="1483";
$offset=1300;
$limit=200;
$soundcloud_url = "http://api.soundcloud.com/users/{$userid}/tracks.json?client_id={$clientid}&offset={$offset}&limit={$limit}";
$tracks_json = file_get_contents($soundcloud_url);
$tracks = json_decode($tracks_json);
foreach ($tracks as $track) {
echo "<pre>";
echo $track->title . ":";
echo $track->permalink_url . "";
echo "</pre>";
}
sI've seen this code is supposed to help (this is in Ruby):
# start paging through results, 100 at a time
tracks = client.get('/tracks', :order => 'created_at', :limit => page_size,
:linked_partitioning => 1)
tracks.each { |t| puts t.title }
However, the first set of results will show and i'll even see the "next_href" at the end of the response, but what are you supposed to do, to make the next set of results show?