通过经纬度获取地址 / 通过地址获取经纬度 Get city name using geolocation

1. 方法一

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Reverse Geocoding</title> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
  var geocoder;

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng)
}

function errorFunction(){
    alert("Geocoder failed");
}

  function initialize() {
    geocoder = new google.maps.Geocoder();
  }

  function codeLatLng(lat, lng) {

    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
        if (results[1]) {
         //formatted address
         alert(results[0].formatted_address)
        //find country name
             for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {

            //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                    //this is the object you are looking for
                    city= results[0].address_components[i];
                    break;
                }
            }
        }
        //city data
        alert(city.short_name + " " + city.long_name)


        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
</script> 
</head> 
<body onload="initialize()"> 

</body> 
</html>

来源:http://stackoverflow.com/a/6798005

2. 结合php

function get_address_by_latlng($latitude,$longitude)
{
    //http://maps.googleapis.com/maps/api/geocode/json?latlng=33.5552692,-116.67418219999999&sensor=true
    
    $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=true');
    return json_decode($json , true);
}

// echo '<pre>'; print_r(get_address_by_latlng(33.5552692,-116.67418219999999));

来源:http://stackoverflow.com/a/6355022

或者:

1. 通过经纬度获取地址

function get_address_by_latlng($latitude,$longitude,$full_address=false)
{
    //http://maps.googleapis.com/maps/api/geocode/json?latlng=33.5552692,-116.67418219999999&sensor=true
    
    $json  = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=true&language=en');
    $infos = json_decode($json , true);
    $infos = $infos['results']; //_printr($infos);
    
    $address['full'] = $infos[0]['formatted_address'];
    
    if($full_address) return $address['full'];
    else 
    {
        $info  = explode(', ', $address['full']);
        $count = count($info);
        
        if(stripos($address['full'], ', USA'))
        {
            $address['usa_key']  = array_search('USA', $info);
            $address['city']     = trim($info[$address['usa_key']-2]);
            $address['province'] = trim($info[$address['usa_key']-1]);
            $address['country']  = trim($info[$address['usa_key']]);
        }
        elseif(stripos($address['full'], ', China'))
        {
            $zhixiashi            = array('Beijing Shi','Tianjin Shi','Shanghai Shi','Chongqing Shi');
            $address['china_key'] = array_search('China', $info);
            $before_china         = trim($info[$address['china_key']-1]);
            
            if(!in_array($before_china, $zhixiashi))
            {
                $address['city']     = trim($info[$address['china_key']-2]);
                $address['province'] = trim($info[$address['china_key']-1]);
                $address['country']  = trim($info[$address['china_key']]);               
            }
            else 
            {
                $address['city']     = trim($info[$address['china_key']-1]);
                $address['province'] = '';
                $address['country']  = trim($info[$address['china_key']]);        
            }
        }
        else 
        {
            $address['city']     = $address['full'];
            $address['province'] = $address['full'];
            $address['country']  = $address['full'];      
        }
    }
    
    return $address;
}

2. 通过地址获取经纬度

function get_lat_long($address)
{
    $address = str_replace(array(' ',','), array('+',''), $address);

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

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

 

 

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.