title: I need to obtain Latitude and Longitude from an Address
body: <p>I'm new here. This is my first post, but I've been a reader for years.</p>
  
  <p>I've been working with HTML, CSS, JS, and PHP for many years, but this has stumped me enough where I'm asking for help online. My problem is that I need to get latitude and longitude coordinates for street addresses I pass in to my code (77 Massachusetts Ave, Cambridge, MA 02139, for example).</p>
  
  <p>I've looked around for weeks (or months) and tried several people's suggestions, which are often written in JS or PHP. I have a valid Google API which should work just fine (others on my team have been using it in Android successfully, and it's marked as being able to work on our site). I've found many examples on multiple sites, but none work.</p>
  
  <p>Everything I tried produces nothing. I realize Google's requirement to use an API is new and some of the examples didn't use one, but even when I apply mine, nothing works. I'm looking for a clean, modern example of how to get this function to work.</p>
  
  <p>Here are a few examples I've tried. They produce nothing and I'm not sure if I can do error reporting to figure out why:</p>
  
  <pre><code>&lt;?php
  function getLatLong($address){
  if (!is_string($address))die("All Addresses must be passed as a string");
  $_url = sprintf('http://maps.googleapis.com/maps/api/geocode/json?address=%s&amp;sensor=true&amp;key=[MY_API_KEY]',rawurlencode($address));
  if($_result = file_get_contents($_url)) {
  $_result = json_decode($_result);
  if($_result !== false || !empty($_result-&gt;results)) {
  return $_result-&gt;results[0]-&gt;geometry-&gt;location;
  }
  }
  return false;
  }
  $address = "77 Massachusetts Ave, Cambridge, MA 02139";
  print_r(getLatLong($address));
  
  $coordinates = getLatLong($address);
  
  $lat = $coordinates['lat'];
  $long = $coordinates['long'];
  echo $lat.", ".$long;
  echo "--";
  ?&gt;
  
  </code></pre>
  
  <pre><code>&lt;?php
  $address = '77 Massachusetts Ave, Cambridge, MA 02139'; // Address
  $apiKey = '[MY_API_KEY]'; // Google maps now requires an API key.
  // Get JSON results from this request
  $geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&amp;sensor=false&amp;key='.$apiKey);
  $geo = json_decode($geo, true); // Convert the JSON to an array
  
  if (isset($geo['status']) &amp;&amp; ($geo['status'] == 'OK')) {
    $latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
    $longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
  }
  ?&gt;
  </code></pre>
  
  <pre><code>    &lt;?php
  $dlocation = "77 Massachusetts Ave, Cambridge, MA 02139";
       // Get lat and long by address         
          $address = $dlocation; // Google HQ
          $prepAddr = str_replace(' ','+',$address);
          $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&amp;sensor=false&amp;key=[MY_API_KEY]');
          $output= json_decode($geocode);
          $latitude = $output-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
          $longitude = $output-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng;
  echo $latitude;
  ?&gt;
  </code></pre>
  
  <p>Any assistance would be appreciated! If you have any example code which can do this, I would certainly like to see it.</p>
  
  <p>Thank you very much,
  BDP</p>