Get Latitude and Longitude Automatically Using PHP, API

Get latitude and longitude automatically using php, API

Use curl instead of file_get_contents:

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

PHP - Get Latitude & Longitude from an address

Suppose you have hidden value for lat and long is
mapLat & mapLong and input field name is location
then:

<html>
<form>
<input type="hidden" name="mapLat">
<input type="hidden" name="mapLong">
<input type="text" name="location">
<input type="submit" name="submit" value="submit">
</form>
</html>

extract($_POST);
if($mapLat =='' && $mapLong ==''){
// Get lat long from google
$latlong = get_lat_long($location); // create a function with the name "get_lat_long" given as below
$map = explode(',' ,$latlong);
$mapLat = $map[0];
$mapLong = $map[1];
}

// function to get the address
function get_lat_long($address){

$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
return $lat.','.$long;
}

I want to get latitude and longitude from Autocomplete Address Form

I have written this script for one of my recent project this works fine for me

<script>

var autocomplete;

function initAutocomplete(){

autocomplete = new google.maps.places.Autocomplete(

document.getElementById('autocomplete-input'), {types: ['geocode']});

autocomplete.addListener('place_changed', getAddressDetails);

}

function getAddressDetails(){

var place = autocomplete.getPlace();

window.lat = place.geometry.location.lat();

window.long = place.geometry.location.lng();

}

function geolocate(){

if (navigator.geolocation){

navigator.geolocation.getCurrentPosition(function(position){

var geolocation = {

lat: position.coords.latitude,

lng: position.coords.longitude

};

var circle = new google.maps.Circle(

{center: geolocation, radius: position.coords.accuracy});

autocomplete.setBounds(circle.getBounds());

});

}

}

</script>

Get latitude and longitude automatically using php, API

Use curl instead of file_get_contents:

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

Using Google API get Latitude, Longitude from places and get specific country for the same(using PHP)

Grab it from formatted_address property. It contains full address, comma separeted with country after last comma. Try with:

$formatted_address = $output->results[0]->formatted_address;

$country = trim(end(explode(',',$formatted_address)));

Getting latitude and longitude of a location using Google API and passing of variables

The latitude and longitude are held in your results[0] but under results[0].location.lat() and results[0].location.lng(). These can be passed back to the html by specifying the element you want to apply them to and populate the html with the information, something like:

document.getElementById('coords').innerHTML(results[0].location.lat() + ', ' + results[0].location.lng());

To add new markers to a map ideally you need first to set up an array to hold the markers you create. Then, when you have the new markers information, push them to the array. This will accomplish two things. One, the markers will be automatically added to the map. Second, if you need to change the markers in any way (perhaps by clearing them from the screen, or deleting them altogether) you can perform an action on the array.

Here's a couple of quick functions related to that (assumes your array is called markers.)

function clearMarkers() { // clears markers but doesn't remove them from the markers array
for (var i = 0, l = this.markers.length; i < l; i++) {
this.markers[i].setMap(null);
}
};

function deleteMarkers() {
this.markers = [];
}

In order to add a marker to the array you need to define it, something along the lines of:

function addMarker(latlng) {
var marker = new google.maps.Marker({
draggable: false,
raiseOnDrag: false,
icon: // icon path
animation: google.maps.Animation.DROP,
position: latlng, // google latlng coords
map: // element holding your map
});
markers.push(marker);
}

Everything you need is available in the Google Maps API.



Related Topics



Leave a reply



Submit