Getting data from JSON (Using Mapquest and PHP) - php

So, I'm trying to pair Google and MapQuest's geocoding abilities because some address aren't able to be geocoded through Google, but they show up on Mapquest so I want to pair them. I was able to get the google results:
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat1 = $output->results[0]->geometry->location->lat;
$lon1 = $output->results[0]->geometry->location->lng;
How do I get the results using MapQuest? I've never used MapQuest so I have no idea how it returns the data and I haven't found anything on here or anywhere that demonstrates retrieving the data...
HELP! Thanks!

You can use Jay Sheth's sample code then get latitude and longitude:
$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={your_key_here}&location=Lancaster,PA');
$jsonArr = json_decode($json);
$lat1 = $jsonArr->results[0]->locations[0]->latLng->lat;
$lon1 = $jsonArr->results[0]->locations[0]->latLng->lng;

this code should start you off:
<?php
//Important: do not pass the callback=xyz parameter (as stated in the docs)
$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={your_key_here}&location=Lancaster,PA');
$jsonArr = json_decode($json);
print_r($jsonArr);
//Access latitude, longitude, etc. from PHP standard object
?>
More info:
http://open.mapquestapi.com/geocoding/

Related

How to do bulk lat/long lookup for google geocoding api in PHP?

I am using this endpoint with CURL to get lat/long for specific address and it working fine but if i need to get it for 1000 records then needs to make 1000 api call
is there any way i can supply json of address and get the lat/long for 1000 records in one call
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,
+Mountain+View,+CA&key=YOUR_API_KEY
create foreach loop
$dataLocations = ["London","Slovakia","Ukraine","Kiev","New York"];
foreach($dataLocations as $location){
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$location;
$data = file_get_contents($url);
$json = json_decode($data, true);
return $json;
}

get variables from Google Maps with JSON

What am I doing wrong here? I want to get the latitude and longitude from a postcode with Google Maps, this is easy enough and I am getting the results but I cannot seem to save them. Here is what I am using. I just need to get and save the lng and lat, but there seems to be more than 1; I want the one in 'location'.
$get_pickup_coords = "https://maps.googleapis.com/maps/api/geocode/xml?&address=cv57bt&sensor=false";
$pickup_data = #file_get_contents($get_pick_coords);
$pickup_result = json_decode($pickup_data, true);
$latitude = $pickup_result[3];
There's a couple of errors in your script. For starters, you're using an xml output and trying to json_decode it. Corrected this by passing the json parameter in the google api instead of xml.
Secondly, $pickup_result[3] won't give you the lat longs, get it as shown below:
$get_pickup_coords = "https://maps.googleapis.com/maps/api/geocode/json?&address=cv57bt&sensor=false";
$pickup_data = file_get_contents($get_pickup_coords);
$pickup_result = json_decode($pickup_data, true);
$lat = $pickup_result['results'][0]['geometry']['location']['lat'];
$long = $pickup_result['results'][0]['geometry']['location']['lng'];
$lat and $long variables contain the latitude and longitude respectively.
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=cv57bt";
$data = json_decode( file_get_contents( $url ) );
$location=$data->results[0]->geometry->location;
echo 'Latitude:',$location->lat,' Longitude:',$location->lng;

Bidimensional array in PHP cant acces nominatim geocoding data

I'm accessing lat and lon via nominatim json and converting it to php array,all the stuff works until i try to acces lon into one array. If i acces to the array int its ok but if i try to get what's inside it dont.
$url = "http://nominatim.openstreetmap.org/search/".$_GET['places']."?format=json&email=gotia.ts#gmai.com";
$placeJSON = file_get_contents($url);
$arrPlace = json_decode($placeJSON);
//$lati = $arrPlace[0]; works
//That does not
$lati = $arrPlace[0]['lat'];
You Try this format
echo $lati = $arrPlace['0']->lat;
Working fine

How find Distance between two addresses through php

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/

php display information from string

I have this code that gets the IP address and Latitude and Longitude of the user. However I am wanting ti display it on a map if it finds the lat and long.
$theirip = $_SERVER['REMOTE_ADDR'];
$fileinfo = file_get_contents("http://api.easyjquery.com/ips/?ip=".$theirip."&full=true");
This outputs a string like this.
{"IP":"000.000.00.000","continentCode":"Unknown","continentName":"Unknown","countryCode2":"Unknown","COUNTRY":"Unknown","countryCode3":"Unknown","countryName":"Unknown","regionName":"Unknown","cityName":"Unknown","cityLatitude":0,"cityLongitude":0,"countryLatitude":0,"countryLongitude":0,"localTimeZone":"Unknown","localTime":"0"}
I am wanting to retrieve just the cityLatitude and cityLongitude from this. I have researched some and tried some different code.
$lat = $fileinfo -> {"cityLatitude"};
but i'm not quite sure how to go about this. Any help would be appreciated!
What you have there is a JSON string, you can use json_decode to pasre it
$obj = json_decode($fileinfo);
$lat = $obj -> {"cityLatitude"};
its a JSONObject... just use the json_decode() to get data from it
no need to decode the JSON, it should work fine.
trying from here its working just fine...
try using your IP instead of $theirip = $_SERVER['REMOTE_ADDR'];
You're using PHP right? It should be
$info = json_decode($fileinfo);
$citylatitude = $info->cityLatitude;

Categories