Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

4

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi muốn lấy địa chỉ, thành phố, tiểu bang và quốc gia trong các biến khác nhau để tôi có thể hiển thị nó một cách riêng biệt nhưng do khác nhau, một nơi nào đó tôi nhận được toàn bộ địa chỉ và ở đâu đó chỉ có tiểu bang và quốc gia, vì vậy tôi không thể nhận được một địa chỉ cụ thể do thay đổi latlong.address, city, state and country in different variables so that I can display it separately but due to different latlong, somewhere I am getting whole address and somewhere only state and country, so I am not able to get a specific address due to changing latlong.

Đây là mã của tôi:

$geoLocation=array();
$URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=8.407168,6.152344&sensor=false';
$data = file_get_contents($URL);
$geoAry = json_decode($data,true);
for($i=0;$i$city,
  'state'=>$state,
  'country'=>$country,
  'address'=>$address
);
print_r($geoLocation);

Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

Hỏi ngày 14 tháng 4 năm 2015 lúc 9:45Apr 14, 2015 at 9:45

Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

Thử bên dưới một:

results[0])) {
    $response = array();
    foreach($json_decode->results[0]->address_components as $addressComponet) {
        if(in_array('political', $addressComponet->types)) {
                $response[] = $addressComponet->long_name; 
        }
    }

    if(isset($response[0])){ $first  =  $response[0];  } else { $first  = 'null'; }
    if(isset($response[1])){ $second =  $response[1];  } else { $second = 'null'; } 
    if(isset($response[2])){ $third  =  $response[2];  } else { $third  = 'null'; }
    if(isset($response[3])){ $fourth =  $response[3];  } else { $fourth = 'null'; }
    if(isset($response[4])){ $fifth  =  $response[4];  } else { $fifth  = 'null'; }

    if( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth != 'null' ) {
        echo "
Address:: ".$first; echo "
City:: ".$second; echo "
State:: ".$fourth; echo "
Country:: ".$fifth; } else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth == 'null' ) { echo "
Address:: ".$first; echo "
City:: ".$second; echo "
State:: ".$third; echo "
Country:: ".$fourth; } else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
City:: ".$first; echo "
State:: ".$second; echo "
Country:: ".$third; } else if ( $first != 'null' && $second != 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
State:: ".$first; echo "
Country:: ".$second; } else if ( $first != 'null' && $second == 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
Country:: ".$first; } } ?>

Đã trả lời ngày 14 tháng 4 năm 2015 lúc 11:04Apr 14, 2015 at 11:04

Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

Shahzad Barkatishahzad BarkatiShahzad Barkati

2.5146 Huy hiệu vàng23 Huy hiệu bạc32 Huy hiệu Đồng6 gold badges23 silver badges32 bronze badges

5

Sử dụng API Google Geocode để có được vị trí từ vĩ độ và kinh độ.

Ở đây tập lệnh PHP đơn giản cho điều đó.

$latitude = 'Insert Latitude';
$longitude = 'Insert Longitude';

$geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false'); 
$output = json_decode($geocodeFromLatLong);
$status = $output->status;
$address = ($status=="OK")?$output->results[1]->formatted_address:'';

Mã nguồn trên được tìm thấy từ đây - Nhận địa chỉ từ vĩ độ và kinh độ bằng cách sử dụng API và PHP của Google Maps

Đã trả lời ngày 12 tháng 4 năm 2016 lúc 7:46Apr 12, 2016 at 7:46

JoygurujoyguruJoyGuru

1.72519 huy hiệu bạc9 huy hiệu đồng19 silver badges9 bronze badges

Kiểm tra API mã hóa địa lý Google tại đây: https://developers.google.com/maps/documentation/geocoding

Trong trường hợp của bạn, vị trí_type được đặt thành "gần đúng", điều đó có nghĩa là kết quả trả về không phải là mã địa lý chính xác, điều đó có nghĩa là bạn sẽ không có toàn bộ địa chỉ.

Đã trả lời ngày 14 tháng 4 năm 2015 lúc 10:16Apr 14, 2015 at 10:16

Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

Tiến sĩ Zdr. ZDr. Z

2253 Huy hiệu bạc19 Huy hiệu đồng3 silver badges19 bronze badges

Tôi đang sử dụng codeigniter và có các yêu cầu tương tự, đây là cách tôi nhận được nó

    

HTML:

   
                        

Php:

        $current_long= $this->input->post('current_long');
        $current_lat=$this->input->post('current_lat');
        $newdata = array(

                   'current_lat'     => $current_lat,
                   'current_long'     => $current_long
               );

               $this->session->set_userdata($newdata);  

Đã trả lời ngày 13 tháng 10 năm 2019 lúc 8:57Oct 13, 2019 at 8:57

FahadkhanfahadkhanFahadKhan

711 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges

Nếu bạn sử dụng laravel thì nó sẽ hữu ích cho bạn

    $latitude=26.12312;
    $longitude=83.77823;
    $geolocation = $latitude.','.$longitude;
    $response = \Http::get('https://maps.googleapis.com/maps/api/geocode/json', [
        'latlng' => $geolocation,
        'key' => 'api key',
        'sensor'    =>  false
    ]);
    $json_decode = json_decode($response);
    if(isset($json_decode->results[0])) {
        $response = array();
        foreach($json_decode->results[0]->address_components as $addressComponet) {
            $response[] = $addressComponet->long_name;
        }
        dd($response);
    }

Vui lòng thay thế khóa API bằng riêng của bạn

Đã trả lời ngày 12 tháng 11 năm 2021 lúc 6:50Nov 12, 2021 at 6:50

Hướng dẫn how to get current longitude and latitude in php - cách lấy kinh độ và vĩ độ hiện tại trong php

Anjani Barnwalanjani BarnwalAnjani Barnwal

1.2641 Huy hiệu vàng15 Huy hiệu bạc22 Huy hiệu đồng1 gold badge15 silver badges22 bronze badges

Các giải pháp trên sẽ không hoạt động vì Google hiện được yêu cầu khóa API để lấy dữ liệu:

Đây là một hàm đơn giản sẽ trả về lat, long cùng với

results[0])) {
    $response = array();
    foreach($json_decode->results[0]->address_components as $addressComponet) {
        if(in_array('political', $addressComponet->types)) {
                $response[] = $addressComponet->long_name; 
        }
    }

    if(isset($response[0])){ $first  =  $response[0];  } else { $first  = 'null'; }
    if(isset($response[1])){ $second =  $response[1];  } else { $second = 'null'; } 
    if(isset($response[2])){ $third  =  $response[2];  } else { $third  = 'null'; }
    if(isset($response[3])){ $fourth =  $response[3];  } else { $fourth = 'null'; }
    if(isset($response[4])){ $fifth  =  $response[4];  } else { $fifth  = 'null'; }

    if( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth != 'null' ) {
        echo "
Address:: ".$first; echo "
City:: ".$second; echo "
State:: ".$fourth; echo "
Country:: ".$fifth; } else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth == 'null' ) { echo "
Address:: ".$first; echo "
City:: ".$second; echo "
State:: ".$third; echo "
Country:: ".$fourth; } else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
City:: ".$first; echo "
State:: ".$second; echo "
Country:: ".$third; } else if ( $first != 'null' && $second != 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
State:: ".$first; echo "
Country:: ".$second; } else if ( $first != 'null' && $second == 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) { echo "
Country:: ".$first; } } ?>
0

function geoLocate($address)
{
    try {
        $lat = 0;
        $lng = 0;

        $data_location = "https://maps.google.com/maps/api/geocode/json?key=".$GOOGLE_API_KEY_HERE."&address=".str_replace(" ", "+", $address)."&sensor=false";
        $data = file_get_contents($data_location);
        usleep(200000);
        // turn this on to see if we are being blocked
        // echo $data;
        $data = json_decode($data);
        if ($data->status=="OK") {
            $lat = $data->results[0]->geometry->location->lat;
            $lng = $data->results[0]->geometry->location->lng;

            if($lat && $lng) {
                return array(
                    'status' => true,
                    'lat' => $lat, 
                    'long' => $lng, 
                    'google_place_id' => $data->results[0]->place_id
                );
            }
        }
        if($data->status == 'OVER_QUERY_LIMIT') {
            return array(
                'status' => false, 
                'message' => 'Google Amp API OVER_QUERY_LIMIT, Please update your google map api key or try tomorrow'
            );
        }

    } catch (Exception $e) {

    }

    return array('lat' => null, 'long' => null, 'status' => false);
}

Đã trả lời ngày 19 tháng 4 năm 2017 lúc 18:51Apr 19, 2017 at 18:51

Trang sứcJewel

2.27323 Huy hiệu bạc23 Huy hiệu đồng23 silver badges23 bronze badges

Làm thế nào tôi có thể nhận được vĩ độ và kinh độ từ địa chỉ trong PHP?

Nhận vĩ độ & kinh độ từ API mã hóa địa lý Google trong PHP..
// API mã hóa địa lý Google. Chìa khóa..
$ apikey = 'khóa API. Đến đây ';.
$ địa chỉ = urlencode ('1600 amphitheater. ....
$ url = "https.: //maps.googleapis. ....
$ resp = json_decode (file_get_contents (....
// Latitude và. Kinh độ (Php 7. ....
$ lat = $ resp ['Kết quả'] [0] ....
$ long = $ resp ['Kết quả'] [0].

Làm thế nào tôi có thể nhận được vị trí hiện tại trong PHP?

Phương thức GetCienPocation () được sử dụng để có được vị trí của khách truy cập và phương thức hiển thị () () được sử dụng để lấy địa chỉ của khách truy cập từ tệp getlocation.php bằng AJAX. Mã HTML: Sau khi nhận được vị trí khách truy cập, địa chỉ sẽ được hiển thị trên trang web (nhịp #Location).getCurrentPosition() method is used to get the visitor's position and showLocation() method is used to getting the visitor's address from the getLocation. php file using Ajax. HTML Code: After getting the visitor position, the address will be shown on the web page ( #location span).

Làm thế nào để tôi tìm thấy vĩ độ và kinh độ hiện tại của tôi?

Android: Mở Google Maps;Nó sẽ phóng to vị trí gần đúng của bạn. Nhấn và giữ trên màn hình để thả một điểm đánh dấu pin. Bấm vào pin bị rơi;Vĩ độ và kinh độ sẽ được hiển thị bên dưới bản đồ.Nếu bạn không có Google Maps, bạn có thể cài đặt ứng dụng GPS miễn phí trước khi truy cập trang web của mình.Open Google Maps; it will zoom to your approximate location. Press and hold on the screen to drop a pin marker. Click on the dropped pin; latitude and longitude will be displayed below the map. If you don't have Google Maps, you can install a free GPS app before heading to your site.

Làm thế nào tôi có thể nhận được vĩ độ và kinh độ từ pincode PHP?

$ geodata) {ném ngoại lệ mới ("lỗi để trích xuất geodata.");} // Trích xuất vĩ độ và kinh độ $ lat = $ geodata-> kết quả [0]-> Hình học-> vị trí-> lat;$ lng = $ geodata-> kết quả [0]-> hình học-> vị trí-> lng;$ geodata = json_decode (file_get_contents ('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$lat.'.'.$lng. ...