Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author kristarella

    (@kristarella)

    I don’t know what context you want to use it in, or how good your PHP is, but if you’ve got the image ID and it’s assigned to $imgID

    if (class_exists('exifography')) {
    	$exif = new exifography();
    	$imgmeta = wp_get_attachment_metadata($imgID);
    	if (isset($imgmeta['image_meta']['latitude'])) {
    				if ($imgmeta['image_meta']['latitude'])
    					$latitude = $imgmeta['image_meta']['latitude'];
    				if ($imgmeta['image_meta']['longitude'])
    					$longitude = $imgmeta['image_meta']['longitude'];
    				if ($imgmeta['image_meta']['latitude_ref'])
    					$lat_ref = $imgmeta['image_meta']['latitude_ref'];
    				if ($imgmeta['image_meta']['longitude_ref'])
    					$lng_ref = $imgmeta['image_meta']['longitude_ref'];
    
    				$lat = $exif->geo_single_fracs2dec($latitude);
    				$lng = $exif->geo_single_fracs2dec($longitude);
    				if ($lat_ref == 'S') { $neg_lat = '-'; } else { $neg_lat = ''; }
    				if ($lng_ref == 'W') { $neg_lng = '-'; } else { $neg_lng = ''; }
    
    				$geo_coords['lat'] = $neg_lat . number_format($lat,6) ;
     				$geo_coords['lng'] = $neg_lng . number_format($lng, 6);
    
    		return $geo_coords;
    	}
    }
    Thread Starter Advanced SEO

    (@jole5)

    Thank you for your effort.

    I tried it and I get nothing. Also I do not how to combine your function and function from above link to get location as plain text.

    In source code of location image (Google map) there is latitude / longitude as decimal numbers. I thought, since there is option to get image (map) of location where image is taken it would be great to get name of that place too.

    When I open source image and copy parts of it (latitude / longitude as decimal numbers) inside PHP code from here:
    https://99webtools.com/php-reverse-geocoding.php

    I get location of image (as text). I think it would be nice to have image of location and text where is it (text returned from Google).

    Plugin Author kristarella

    (@kristarella)

    Please provide any code you are using and tell me which files you are using it in so I can see how to best help.

    Thread Starter Advanced SEO

    (@jole5)

    I wish to get that info on post page (single.php file).

    I tried this, and it is working (if I manually enter latitude and longitude, as decimal numbers).

    <?
    function getaddress($lat,$lng)
    {
    $url = 'https://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;
    }
    ?>
    <?
    $lat= 26.754347; //latitude
    $lng= 81.001640; //longitude
    $address= getaddress($lat,$lng);
    if($address)
    {
    echo $address;
    }
    else
    {
    echo "Not found";
    }
    ?>

    On your site I found function to get get latitude / longitude as pretty numbers like:
    Latitude: 26° 45′ 15.6486″
    Longitude: 81° 0′ 5.9034″

    Very useful too, thanks.

    What is my plan?

    I wish to display more information, standard is just image map of location. I added location as latitude/longitude, and would be great to get location as text too (in example coordinates above it is: Nagram – Nilmatha Road…).

    Plugin Author kristarella

    (@kristarella)

    Sorry for the delay…

    We can adapt code from this post:

    function show_numeric_geo($imgID) {
    	if (class_exists('exifography')) {
    		$exif = new exifography();
    		if (empty($imgID))
    			$imgID = get_post_thumbnail_id();
    		$allmeta = wp_get_attachment_metadata($imgID);
    		$imgmeta = $allmeta['image_meta'];
    
    		if (isset($imgmeta['latitude'])) {
    			if ($imgmeta['latitude'])
    				$latitude = $imgmeta['latitude'];
    			if ($imgmeta['longitude'])
    				$longitude = $imgmeta['longitude'];
    			if ($imgmeta['latitude_ref'])
    				$lat_ref = $imgmeta['latitude_ref'];
    			if ($imgmeta['longitude_ref'])
    				$lng_ref = $imgmeta['longitude_ref'];
    
    			$lat = $exif->geo_single_fracs2dec($latitude);
    			$lng = $exif->geo_single_fracs2dec($longitude);
    			if ($lat_ref == 'S') { $neg_lat = '-'; } else { $neg_lat = ''; }
    			if ($lng_ref == 'W') { $neg_lng = '-'; } else { $neg_lng = ''; }
    
    			$lat = $neg_lat . number_format($lat,6);
    			$lng = $neg_lng . number_format($lng, 6);
    		}
    	}
    
    	echo getaddress($lat,$lng);
    }

    The above function combined with your reverse geo lookup will display the address. You can add the image ID as a parameter. E.g., show_numeric_geo(123); Or, if you don’t use an image ID it will use the featured image ID.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get address from latitude / longitude?’ is closed to new replies.