<script>不会从给定的映射地址 jquery 或 javascript 中获取 Lat 和 Long



我想从给定的地址找到纬度和液化天然气,但我找不到相同的,你能帮我解决这个查询吗

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
 var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': "patan"}, function(results, status) {
    alert(status)
      if (status == google.maps.GeocoderStatus.OK) {
      alert(results[0].geometry.location.lat());
        //$('.push-down').text("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
      } else {
        $('.push-down').text("Something got wrong " + status);
      }
    });
</script>

请尝试以下代码:-

<div class="push-down"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
 <script>
 var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': "patan"}, function(results, status) {
    //alert(status)
      if (status == google.maps.GeocoderStatus.OK) {
      var Lat = results[0].geometry.location.lat();
      var Lng = results[0].geometry.location.lng();
       $('.push-down').html("latitude: " + Lat + " <br/>longitude:  " +Lng); 
      } else {
        $('.push-down').text("Something got wrong " + status);
      }
    });
  </script>

希望对:)有所帮助

由于您的问题也被标记为php,因此我建议您使用Google Geocode API Web Service。我发现它比 js API 更容易使用。

下面是一个示例代码:

<?php
//  Request URL for Google Geocode API, result type = json, language = hu-HU, region = hu-HU
//  Change result type, language, region to your needs
define('REQ_URL', 'https://maps.googleapis.com/maps/api/geocode/json?language=hu&region=hu');
//  Your Google API key goes here
define('API_KEY', 'your-google-api-key-goes-here');
//  Address you want to geocode
//  Needs to be url encoded
$address = 'address-you-want-to-geocode-goes-here';
$address = urlencode($address);
//  Parsing full request url
$request_url = REQ_URL . '&address=' . $address . '&key=' . API_KEY;
//  Query Google API for result
$response = file_get_contents($request_url);
$resp = json_decode($response, true);
//  Result array
print '<pre>';
print_r($resp);
print '</pre>';

我希望,我能有所帮助。

最新更新