I am trying to encode variables for the URL for the google maps geocoding API with PHP. I have used both urlencode and rawurlencode and I get the same result for
Székesfehérvár, Hungary
PHP returns
Sz%E9kesfeh%E9rv%E1r%2C%20Hungary
Which cannot be decoded by any of the online decoders.
When I encode with the online encoder tools I get:
Sz%C3%A9kesfeh%C3%A9rv%C3%A1r%2C+Hungary
When I plug that in directly to the Google Maps geocoder URL, it is successful. The PHP function fails every time.
This is the code I am using:
function getLatandLong($landmark,$city,$state)
{
global $lat;
global $long;
global $city;
if (!empty($landmark)) { $encoded_vars = "$landmark,$city,+$state";
} else { $encoded_vars = "$city, $state"; }
$encodedVars = urlencode($encoded_vars);
$doc = new DOMDocument();
$doc->load("http://maps.google.com/maps/api/geocode/xml?address=$encodedVars&sensor=false"); //input address
Well, I tried
$encodedVars = utf8_encode($encoded_vars);
And that seems to be working, even though the echo shows (probably because I don;t have the right page encoding set on my catch page:
Székesfehérvár,+Hungary
Related
I want to change real-time coordinates captured from my google maps to address.
I've tried the following code:
<?php
$lat="9.102097";
$long="-40.187988";
geo2address($lat,$long);
function geo2address($lat,$long) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
$curlData=file_get_contents( $url);
$address = json_decode($curlData);
$a=$address->results[0];
return explode(",",$a->formatted_address);
print_r($address);
}
?>
P.S. I'm testing with a fixed coordinates but will capture coordinates from my application when I success
I referenced the code from How to convert GPS coordinates to a full address with php? but it is not working.
There are few issues with the $url itself, such as:
You're using http in the URL, whereas you should use https.
You're not using key parameter in the URL. Sign up for an API KEY and use it in the URL.
Here's an example of one such URL:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY`
Here's the documentation of reverse geocoding:
https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding
So your code should be like this:
$lat="40.714224";
$long="-73.961452";
geo2address($lat,$long);
function geo2address($lat,$long) {
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&key=YOUR_API_KEY";
$json = json_decode(file_get_contents($url), true);
$a = $json['results'][0]['formatted_address'];
print_r(explode(",",$a));
}
Few points to note here is:
The latitude and longitude are taken from the documentation itself.
Pass the second parameter as true in the json_decode() function to convert it into associative array. It will be then easy for you to access individual components.
Begginer here, people. Could anybody suggest any kind of solution? I've an user inputed text.
First of all I check if the text has any urls:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/','<a class="post_link"
href="$0">$0</a>',$post);
And after that I need to retrieve that url and put as a variable($url) to this function:
$short=make_bitly_url('$url','o_6sgltp5sq4as','R_f5212f1asdads1cee780eed00d2f1bd2fd794f','xml');
And finally, echo both url and user's text. Thanks in advance for ideas and critiques.
I've tried something like that:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/e',$url,$post){
$shorten = make_bitly_url($url,'o_6sgltpmm5sq4','R_f5212f11cee780ekked00d2f1bd2fd794f','json');
return '<a class="post_link" href="$shorten">$shorten</a>';
};
But even for me it looks some kind of nonsense.
Bitly does have an API available for use. You should check out API Documentation
Here's how to use the bit.ly API from PHP:
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
/* usage */
$short = make_bitly_url('http://davidwalsh.name','davidwalshblog','R_96acc320c5c423e4f5192e006ff24980','json');
echo 'The short URL is: '.$short;
// returns: http://bit.ly/11Owun
Source: David Walsh article
HOWEVER, if you wanted to create your own URL shortening system (similar to bit.ly -- and surprisingly easy to do), here is an 8-part tutorial from PHPacademy on how to do that:
Difficulty level: beginner / intermediate
Each video is approx ten minutes.
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8
i am looking for some help to find the distance between two address through php
as i can not use the google maps api. so get a way from this post
Distance between two addresses
but i need to know how can send the request using URL pattern
and grab the repose to save them in database.
http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false
thanks for any suggestion.
///////////////
After these answers i am there
$customer_address_url = urlencode($customer_address);
$merchant_address_url = urlencode($merchant_address);
$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";
$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
XML response is visible when i put this url : http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false
but can not printing through
echo "<pre>"; print_r($data); exit;
You can try this..
$url = "http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false";
$data = file_get_contents($url);
Now $data contains resulting xml data. You can parse this xml data using..
http://php.net/manual/simplexml.examples.php
use the haversine formula
Also check https://developers.google.com/maps/documentation/distancematrix/
Not getting data back into flash from php that queries mysql data, think the problem is with my as3 code here?
The php works, the as3 posts to the php ok, its the returning of the variables to as3 that I am unsure about and seems to be the problem?
public static function MineData():void{
var myRequest:URLRequest = new URLRequest("login.php");
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
var myVariables:URLVariables = new URLVariables();
myVariables.School_name_test = String(PostToPHP3.Temp_flash_TI_School_name_test);
myRequest.method = URLRequestMethod.POST;
myRequest.data = myVariables;
function onLoaded(event:Event) {
var myURLVariables:URLVariables = new URLVariables(event.target.data);
DT_display_string_teacher_login_teacher_first_name = myURLVariables.mined_teacher_first_name;
Main.listeningFORPortalteacherlogin.tellMainPortalteacherlogin();
}
myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);
}
Kindest regards
Within your PHP file, ensure you're only echoing back the values which should be returned to Flash. The format for this returned string is:
VarName=Value
With any further values appended to the same String using the & operator to separate them:
VarName=Value&VarName2=Value2
And so on. See the following example for how you might need to use it:
<?php
echo "mined_teacher_first_name=" . $FIRST_NAME_RETURNED_FROM_SQL;
?>
This example obviously doesn't include all the PHP to retrieve the data from the database but that's how you get the data back to Flash. If you have a very large amount of data, you can also output an XML file from PHP and then parse that from within Flash.
I'll also add that your Flash code for extracting these values looks perfectly fine, although it's not absolutely necessary to convert them to a URLVariables object, you can in fact, access them directly from event.data if you so choose:
function onLoaded(event:Event) {
DT_display_string_teacher_login_teacher_first_name = event.target.data.mined_teacher_first_name;
Main.listeningFORPortalteacherlogin.tellMainPortalteacherlogin();
}
I have made a site with codeigniter and am using the googlemaps v3 library to add markers to a map. I get the locations from my database as an array of objects, which I then loop through in a foreach adding the location to the map each time. If i enter a string as the infowindow_content everything is fine and the info bubble pops up with the string in when a marker is clicked. When I use a property of the location object though the map doesn't load.
Here is some example code:
$this->load->library('Googlemaps');
$towers = $this->towers_model->get_towers();
$config['center'] = '18.557394170647473, -9.31640625';
$config['zoom'] = '2';
$this->googlemaps->initialize($config);
foreach($towers as $tower) {
$marker = array();
$marker['position'] = $tower->Lat.', '.$tower->Long;
$marker['infowindow_content'] = '<p>'.$tower->Dedicn.'</p>';
$marker['icon'] = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|9999FF|000000';
$this->googlemaps->add_marker($marker);
}
$this->data['map'] = $this->googlemaps->create_map();
$this->template->write('scripts', $this->data['map']['js']);
$this->template->write_view('content', 'towers/view_all', $this->data);
$this->template->render();
As you can see I am trying to output $tower->Dedicn and when I do the map just won't load. I have echoed it out thought and its definately defined and contains content etc etc.
Ahhh I found out why! It's because some of the results had speech marks in them which interfered with the javascript so had to use str_replace to replace " with \" to escape them!