Centre Google Map marker on embeded map - php

Cannot get this to work for the life of me, and I can't figure out why.
Ok...
On a wordpress post i have a custom field 'Post Code'. The script I have picks up the value of this field, runs it past google to get Lat and Long values and embeds the map. This is what I have:
$postcode = urlencode( get_field("tutor_post_code")); // post code to look up in this case status however can easily be retrieved from a database or a form post
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
$xml = simplexml_load_file($request_url) or die("url not loading");// XML request
$status = $xml->status;// GET the request status as google's api can return several responses
if ($status=="OK") {
//request returned completed time to get lat / lang for storage
$lat = $xml->result->geometry->location->lat;
$long = $xml->result->geometry->location->lng;
echo "$lat,$long"; //spit out results or you can store them in a DB if you wish
}
if ($status=="ZERO_RESULTS") {
//indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
}
if ($status=="OVER_QUERY_LIMIT") {
//indicates that you are over your quota of geocode requests against the google api
}
if ($status=="REQUEST_DENIED") {
//indicates that your request was denied, generally because of lack of a sensor parameter.
}
if ($status=="INVALID_REQUEST") {
//generally indicates that the query (address or latlng) is missing.
}
echo '<iframe width="100%" height="400" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=';
echo get_field("tutor_post_code");
echo '&zoom=13&center=';
echo "$lat,$long";
echo '&key=AIzaSyB8LLFEJV_Or1sj_u1PGKw12n6leDKND3o"></iframe>';
Any ideas why it's not centering the marker on the map?
Miro

I tried your code and it seems to be working fine.
One possibility is http://maps.googleapis.com/maps/api/geocode/xml?address=is returning a different result than your iframe Google Maps. (This does happen sometime.)
Like #Dr.Molle said, you don't really need to do center as it is default to center to the q point, unless you wants a different center.

Related

Piwik tracking API returns GIF89

I guess I'm missing something basic here, but when I try out the example from here (adapted to my context):
<?php
$piwikTracker = new PiwikTracker( $idSite = {$IDSITE} );
// Specify an API token with at least Admin permission, so the Visitor IP address can be recorded
// Learn more about token_auth: https://piwik.org/faq/general/faq_114/
$piwikTracker->setTokenAuth('my_token_auth_value_here');
// You can manually set the visitor details (resolution, time, plugins, etc.)
// See all other ->set* functions available in the PiwikTracker.php file
$piwikTracker->setResolution(1600, 1400);
// Sends Tracker request via http
$piwikTracker->doTrackPageView('Document title of current page view');
// You can also track Goal conversions
$result = $piwikTracker->doTrackGoal($idGoal = 1, $revenue = 42);
?>
the $result variable contains
GIF89a�����������!�����,�������D�;
which I'm told is a small tracking GIF. But why on earth would the API return a tracking GIF? Shouldn't it rather be a status result about success/failure?

PUT / Soundcloud API with cURL

I'm using this code to get a user but how do it "like" a track via a PUT request in cURL & PHP?
// build our API URL
$url = "http://api.soundcloud.com/resolve.json?"
. "url=http://soundcloud.com/"
. "USERNAME-HERE"
. "&client_id=CLIENT-ID-HERE";
// Grab the contents of the URL
$user_json = file_get_contents($url);
// Decode the JSON to a PHP Object
$user = json_decode($user_json);
// Print out the User ID
echo $user->id;
See SoundCloud API Documentation - /users.
You can favourite a track for a user with a PUT request in the following format:
/users/{user_id}/favorites/{track_id}
If you don't know how to make the cURL request, Google it - there are hundreds of tutorials and answers on StackOverflow. Here is one example:
Querying API through Curl/PHP

Calculate address from latitude and longitude in google API (More than one address calculation in one request)

This function for Reveres Geocoding works well for me. I have to display the address in the information window of a marker, and there are more then 200 in one map. I built the code below and it works perfectly. However, Google's API request limit of 2500/day gets exceeded easily.
Is there any way in I can bulk request Google's API?
Our project is currently a small one, so buying more requests/day isn't an option for us.
<?php
function getaddress($lat,$lng)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = #file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->results[0]->formatted_address;
else
return false;
}
?>
No, you can't "bulk query" a bunch of addresses and have it only count as one request when using the standard API. Each lookup counts as a request.
There are other APIs that don't have limits, or allow more lookups, like MapQuest. If you're hitting the daily limit with Google, try using another API.

Using the Google maps API with JavaScript or PHP?

i've read that you can use either javascript or php with google maps api. so what are the pros and cons for each of them?
and if i got the geocodes stored in a database. should i get them with ajax and process them with javascript or should i use php?
it says in the FAQ that 15000 requests are allowed per day per ip. does this mean that EACH user has to run 15000 requests a day if im using javascript? sounds a lot. but if im using php instead, is it from the server's ip only, and thus 15000 for ALL users?
would be great if someone could shed a light on this topic.
The Google Maps API is a JavaScript library. However Google offers its Geocoding Services through a client-side API in JavaScript and though a server-side web service.
This is an example on how to use the JavaScript geocoder:
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
geocoder.getLatLng(
"London, UK",
function(point) {
if (point) {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
);
}
}
The following is an example showing how to get the latitude and longitude of an address on the server-side using php:
$url = 'http://maps.google.com/maps/geo?q=London,+UK&output=csv&sensor=false';
$data = #file_get_contents($url);
$result = explode(",", $data);
echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude
You understood the free geocoding quota correctly. Server-side geocoding is limited to 15k requests per day per server, while client-side geocoding is limited to 15k requests per day per client. You would need the Google Maps API Premier to increase these limits.
I think you are confused. The only part of the API that you can use PHP with is the geocoding api - http://code.google.com/apis/maps/documentation/geocoding/index.html
Assuming you are asking whether you should do your geocoding with javascript or a server side language like PHP, best practice is to cache whatever geocoding you can into some sort of persistance layaer (xml/db/whatever) and minimise the number of client side geocode requests (because of the delay it will introduce)

Reverse Geocoding With Google Map API And PHP To Get Nearest Location Using Lat,Long coordinates

I need a function to get an nearest address or city from coordinates(lat,long) using google map api reverse geocoding and php... Please give some sample code
You need to use the getLocations method on the GClientGeocoder object in the Google Maps API
var point = new GLatLng (43,-75);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) {
// access the address from the placemarks object
alert (result.address);
});
EDIT: Ok. You are doing this stuff server side. This means you need to use the HTTP Geocoding service. To do this you will need to make an HTTP request using the URL format described in the linked article. You can parse the HTTP response and pull out the address:
// set your API key here
$api_key = "";
// format this string with the appropriate latitude longitude
$url = 'http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&sensor=true_or_false&key=' . $api_key;
// make the HTTP request
$data = #file_get_contents($url);
// parse the json response
$jsondata = json_decode($data,true);
// if we get a placemark array and the status was good, get the addres
if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
{
$addr = $jsondata ['Placemark'][0]['address'];
}
N.B. The Google Maps terms of service explicitly states that geocoding data without putting the results on a Google Map is prohibited.
You can display Geocoding API results on a Google Map, or without a
map. If you want to display Geocoding API results on a map, then these
results must be displayed on a Google Map. It is prohibited to use
Geocoding API data on a map that is not a Google map.

Categories