Cách tìm vị trí gần nhất bằng vĩ độ và kinh độ trong JavaScript

Trang này mô tả dịch vụ phía máy khách có sẵn với Maps JavaScript API. Nếu bạn muốn làm việc với các dịch vụ web của Google Maps trên máy chủ của mình, hãy xem Nút. js Ứng dụng khách cho Dịch vụ Google Maps. Trang tại liên kết đó cũng giới thiệu Máy khách Java, Máy khách Python và Máy khách Go cho Dịch vụ Google Maps

Tổng quan

Ngoài ra, hãy xem Tham chiếu API JavaScript của Maps. mã hóa địa lý

Mã hóa địa lý là quá trình chuyển đổi địa chỉ (như "1600 Amphitheatre Parkway, Mountain View, CA") thành tọa độ địa lý (như vĩ độ 37. 423021 và kinh độ -122. 083739), mà bạn có thể sử dụng để đánh dấu hoặc định vị bản đồ

Mã hóa địa lý ngược là quá trình chuyển đổi tọa độ địa lý thành địa chỉ mà con người có thể đọc được (xem phần )

Bạn cũng có thể sử dụng trình mã hóa địa lý để tìm địa chỉ cho một

API JavaScript của Maps cung cấp một lớp Trình mã hóa địa lý để mã hóa địa lý và đảo ngược mã hóa địa lý một cách linh hoạt từ đầu vào của người dùng. Thay vào đó, nếu bạn muốn mã hóa địa lý các địa chỉ tĩnh, đã biết, hãy xem dịch vụ web Mã hóa địa lý

Bắt đầu

Trước khi sử dụng dịch vụ Mã hóa địa lý trong API JavaScript của Maps, trước tiên hãy đảm bảo rằng API mã hóa địa lý được bật trong Google Cloud Console, trong cùng một dự án mà bạn đã thiết lập cho API JavaScript của Maps

Để xem danh sách các API đã bật của bạn

  1. Truy cập Google Cloud Console
  2. Nhấp vào nút Chọn dự án, sau đó chọn cùng một dự án mà bạn đã thiết lập cho API JavaScript của Maps và nhấp vào Mở
  3. Từ danh sách API trên Trang tổng quan, hãy tìm API mã hóa địa lý
  4. Nếu bạn thấy API trong danh sách, thì bạn đã hoàn tất. Nếu API không được liệt kê, hãy kích hoạt nó
    1. Ở đầu trang, chọn BẬT API để hiển thị tab Thư viện. Ngoài ra, từ menu bên trái, chọn Thư viện
    2. Tìm kiếm Geocoding API, sau đó chọn nó từ danh sách kết quả
    3. Chọn BẬT. Khi quá trình kết thúc, API mã hóa địa lý sẽ xuất hiện trong danh sách API trên Trang tổng quan

Giá cả và chính sách

định giá

Có hiệu lực từ ngày 16 tháng 7 năm 2018, gói giá mới trả theo mức sử dụng đã có hiệu lực cho Bản đồ, Tuyến đường và Địa điểm. Để tìm hiểu thêm về giới hạn sử dụng và định giá mới cho việc bạn sử dụng dịch vụ Mã hóa địa lý JavaScript, hãy xem phần Sử dụng và thanh toán cho API mã hóa địa lý

chính sách

Việc sử dụng dịch vụ Mã hóa địa lý phải tuân theo các chính sách được mô tả cho API mã hóa địa lý

Yêu cầu mã hóa địa lý

Việc truy cập dịch vụ Mã hóa địa lý là không đồng bộ, do API Google Maps cần thực hiện lệnh gọi đến máy chủ bên ngoài. Vì lý do đó, bạn cần chuyển một phương thức gọi lại để thực thi sau khi hoàn thành yêu cầu. Phương thức gọi lại này xử lý (các) kết quả. Lưu ý rằng trình mã hóa địa lý có thể trả về nhiều kết quả

Bạn truy cập dịch vụ mã hóa địa lý API Google Maps trong mã của mình thông qua đối tượng hàm tạo

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
3. Phương thức
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
4 khởi tạo một yêu cầu tới dịch vụ mã hóa địa lý, chuyển cho nó một đối tượng
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
5 có chứa các điều khoản đầu vào và một phương thức gọi lại để thực thi khi nhận được phản hồi

Đối tượng

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
5 chứa các trường sau

{
 address: string,
 location: LatLng,
 placeId: string,
 bounds: LatLngBounds,
 componentRestrictions: GeocoderComponentRestrictions,
 region: string
}

Thông số bắt buộc. Bạn phải cung cấp một và chỉ một trong các trường sau

  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    7 — Địa chỉ mà bạn muốn mã hóa địa lý
    hoặc
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    8 —
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    9 (hoặc
      var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    0) mà bạn muốn lấy địa chỉ gần nhất, con người có thể đọc được. Trình mã hóa địa lý thực hiện mã địa lý đảo ngược. Xem để biết thêm thông tin
    hoặc
      var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    1 — ID địa điểm của địa điểm mà bạn muốn lấy địa chỉ gần nhất, con người có thể đọc được. Xem thêm về

thông số tùy chọn

  •   var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    2 —
      var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    3 trong đó thiên vị mã địa lý cho kết quả nổi bật hơn. Tham số
      var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    2 sẽ chỉ ảnh hưởng chứ không hạn chế hoàn toàn kết quả từ bộ mã hóa địa lý. Xem thêm thông tin về bên dưới
  •   var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    5 — Được sử dụng để giới hạn kết quả trong một khu vực cụ thể. Xem thêm thông tin về bên dưới
  •   var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    6 — Mã vùng, được chỉ định dưới dạng được chỉ định dưới dạng thẻ phụ vùng Unicode gồm hai ký tự (không phải số). Trong hầu hết các trường hợp, các thẻ này ánh xạ trực tiếp tới các giá trị hai ký tự ccTLD ("miền cấp cao nhất") quen thuộc. Tham số
      var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    6 sẽ chỉ ảnh hưởng chứ không hạn chế hoàn toàn kết quả từ bộ mã hóa địa lý. Xem thêm thông tin về bên dưới

Phản hồi mã hóa địa lý

Dịch vụ Mã hóa địa lý yêu cầu phương thức gọi lại để thực thi khi truy xuất kết quả của trình mã hóa địa lý. Cuộc gọi lại này phải truyền hai tham số để giữ mã

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
8 và mã
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
9, theo thứ tự đó

Kết quả mã hóa địa lý

Đối tượng

{
  "types":["locality","political"],
  "formatted_address":"Winnetka, IL, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["locality","political"]
  },{
    "long_name":"Illinois",
    "short_name":"IL",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location":[ -87.7417070, 42.1083080],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
}
0 đại diện cho một kết quả mã hóa địa lý duy nhất. Yêu cầu mã địa lý có thể trả về nhiều đối tượng kết quả

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}

Những lĩnh vực này được giải thích dưới đây

  • {
      "types":["locality","political"],
      "formatted_address":"Winnetka, IL, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["locality","political"]
      },{
        "long_name":"Illinois",
        "short_name":"IL",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location":[ -87.7417070, 42.1083080],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
    }
    
    1 là một mảng cho biết loại địa chỉ của kết quả trả về. Mảng này chứa một tập hợp gồm 0 hoặc nhiều thẻ xác định loại tính năng được trả về trong kết quả. Ví dụ: mã địa lý của "Chicago" trả về "địa phương" cho biết "Chicago" là một thành phố và cũng trả về "chính trị" cho biết đó là một thực thể chính trị. Xem thêm thông tin về bên dưới
  • {
      "types":["locality","political"],
      "formatted_address":"Winnetka, IL, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["locality","political"]
      },{
        "long_name":"Illinois",
        "short_name":"IL",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location":[ -87.7417070, 42.1083080],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
    }
    
    2 là một chuỗi chứa địa chỉ con người có thể đọc được của vị trí này

    Thường thì địa chỉ này tương đương với địa chỉ bưu chính. Lưu ý rằng một số quốc gia, chẳng hạn như Vương quốc Anh, không cho phép phân phối địa chỉ bưu chính thực do hạn chế cấp phép

    Địa chỉ được định dạng bao gồm một hoặc nhiều thành phần địa chỉ một cách hợp lý. Ví dụ: địa chỉ "111 8th Avenue, New York, NY" bao gồm các thành phần sau. "111" (số đường), "8th Avenue" (tuyến đường), "New York" (thành phố) và "NY" (tiểu bang của Hoa Kỳ)

    Không phân tích cú pháp địa chỉ được định dạng theo chương trình. Thay vào đó, bạn nên sử dụng các thành phần địa chỉ riêng lẻ mà phản hồi API bao gồm ngoài trường địa chỉ được định dạng

  • {
      "types":["locality","political"],
      "formatted_address":"Winnetka, IL, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["locality","political"]
      },{
        "long_name":"Illinois",
        "short_name":"IL",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location":[ -87.7417070, 42.1083080],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
    }
    
    3 là một mảng chứa các thành phần riêng biệt áp dụng cho địa chỉ này

    Mỗi thành phần địa chỉ thường chứa các trường sau

    • {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      1 là một mảng cho biết loại thành phần địa chỉ. Xem danh sách các loại được hỗ trợ
    • {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      5 là mô tả toàn văn hoặc tên của thành phần địa chỉ do Bộ mã hóa địa lý trả về
    • {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      6 là tên văn bản viết tắt của thành phần địa chỉ, nếu có. Ví dụ: một thành phần địa chỉ cho tiểu bang Alaska có thể có
      {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      5 của "Alaska" và
      {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      6 của "AK" bằng cách sử dụng 2 chữ cái viết tắt của bưu chính

    Lưu ý các sự kiện sau về mảng

    {
      "types":["locality","political"],
      "formatted_address":"Winnetka, IL, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["locality","political"]
      },{
        "long_name":"Illinois",
        "short_name":"IL",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location":[ -87.7417070, 42.1083080],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
    }
    
    3

    • Mảng các thành phần địa chỉ có thể chứa nhiều thành phần hơn
      {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      2
    • Mảng không nhất thiết phải bao gồm tất cả các thực thể chính trị có chứa địa chỉ, ngoại trừ những thực thể có trong
      {
        "types":["locality","political"],
        "formatted_address":"Winnetka, IL, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["locality","political"]
        },{
          "long_name":"Illinois",
          "short_name":"IL",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location":[ -87.7417070, 42.1083080],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
      }
      
      2. Để truy xuất tất cả các thực thể chính trị có chứa một địa chỉ cụ thể, bạn nên sử dụng mã hóa địa lý ngược, chuyển vĩ độ/kinh độ của địa chỉ làm tham số cho yêu cầu
    • Định dạng của phản hồi không được đảm bảo giữ nguyên giữa các yêu cầu. Cụ thể, số lượng
      {
        "types":["sublocality","political"],
        "formatted_address":"Winnetka, California, USA",
        "address_components":[{
          "long_name":"Winnetka",
          "short_name":"Winnetka",
          "types":["sublocality","political"]
        },{
          "long_name":"Los Angeles",
          "short_name":"Los Angeles",
          "types":["administrative_area_level_3","political"]
        },{
          "long_name":"Los Angeles",
          "short_name":"Los Angeles",
          "types":["administrative_area_level_2","political"]
        },{
          "long_name":"California",
          "short_name":"CA",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "geometry":{
          "location": [34.213171,-118.571022],
          "location_type":"APPROXIMATE"
        },
        "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
      }
      
      2 thay đổi tùy theo địa chỉ được yêu cầu và có thể thay đổi theo thời gian đối với cùng một địa chỉ. Một thành phần có thể thay đổi vị trí trong mảng. Loại thành phần có thể thay đổi. Một thành phần cụ thể có thể bị thiếu trong phản hồi sau này

    Xem thêm thông tin về bên dưới

  • {
      "types":["sublocality","political"],
      "formatted_address":"Winnetka, California, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["sublocality","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_3","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"California",
        "short_name":"CA",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location": [34.213171,-118.571022],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
    }
    
    3 chỉ ra rằng trình mã hóa địa lý đã không trả lại kết quả khớp chính xác cho yêu cầu ban đầu, mặc dù nó có thể khớp với một phần của địa chỉ được yêu cầu. Bạn có thể muốn kiểm tra yêu cầu ban đầu về lỗi chính tả và/hoặc địa chỉ không đầy đủ

    Các đối sánh một phần thường xảy ra nhất đối với các địa chỉ đường phố không tồn tại trong địa phương mà bạn chuyển đến trong yêu cầu. Các kết quả trùng khớp một phần cũng có thể được trả lại khi một yêu cầu khớp với hai hoặc nhiều vị trí trong cùng một địa phương. Ví dụ: "Hillpar St, Bristol, UK" sẽ trả về kết quả khớp một phần cho cả Phố Henry và Phố Henrietta. Lưu ý rằng nếu một yêu cầu bao gồm một thành phần địa chỉ sai chính tả, thì dịch vụ mã hóa địa lý có thể đề xuất một địa chỉ thay thế. Các đề xuất được kích hoạt theo cách này cũng sẽ được đánh dấu là khớp một phần

  • {
      "types":["sublocality","political"],
      "formatted_address":"Winnetka, California, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["sublocality","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_3","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"California",
        "short_name":"CA",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location": [34.213171,-118.571022],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
    }
    
    4là mã định danh duy nhất của một địa điểm, có thể được sử dụng với các API Google khác. Ví dụ: bạn có thể sử dụng
    {
      "types":["sublocality","political"],
      "formatted_address":"Winnetka, California, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["sublocality","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_3","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"California",
        "short_name":"CA",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location": [34.213171,-118.571022],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
    }
    
    4 với thư viện API Google Địa điểm để nhận thông tin chi tiết về doanh nghiệp địa phương, chẳng hạn như số điện thoại, giờ mở cửa, đánh giá của người dùng, v.v. Xem tổng quan về ID địa điểm
  • {
      "types":["sublocality","political"],
      "formatted_address":"Winnetka, California, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["sublocality","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_3","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"California",
        "short_name":"CA",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location": [34.213171,-118.571022],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
    }
    
    6 là một mảng biểu thị tất cả các địa phương có trong một mã bưu chính và chỉ xuất hiện khi kết quả là một mã bưu chính chứa nhiều địa phương
  • {
      "types":["sublocality","political"],
      "formatted_address":"Winnetka, California, USA",
      "address_components":[{
        "long_name":"Winnetka",
        "short_name":"Winnetka",
        "types":["sublocality","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_3","political"]
      },{
        "long_name":"Los Angeles",
        "short_name":"Los Angeles",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"California",
        "short_name":"CA",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"United States",
        "short_name":"US",
        "types":["country","political"]
      }],
      "geometry":{
        "location": [34.213171,-118.571022],
        "location_type":"APPROXIMATE"
      },
      "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
    }
    
    7 chứa các thông tin sau

    • results[]: {
       types[]: string,
       formatted_address: string,
       address_components[]: {
         short_name: string,
         long_name: string,
         postcode_localities[]: string,
         types[]: string
       },
       partial_match: boolean,
       place_id: string,
       postcode_localities[]: string,
       geometry: {
         location: LatLng,
         location_type: GeocoderLocationType
         viewport: LatLngBounds,
         bounds: LatLngBounds
       }
      }
      
      8 chứa giá trị kinh độ, vĩ độ được mã hóa địa lý. Lưu ý rằng chúng tôi trả lại vị trí này dưới dạng đối tượng
      results[]: {
       types[]: string,
       formatted_address: string,
       address_components[]: {
         short_name: string,
         long_name: string,
         postcode_localities[]: string,
         types[]: string
       },
       partial_match: boolean,
       place_id: string,
       postcode_localities[]: string,
       geometry: {
         location: LatLng,
         location_type: GeocoderLocationType
         viewport: LatLngBounds,
         bounds: LatLngBounds
       }
      }
      
      9, không phải dưới dạng chuỗi được định dạng
    • {
        "types":["locality","political"],
        "formatted_address":"Toledo, OH, USA",
        "address_components":[{
          "long_name":"Toledo",
          "short_name":"Toledo",
          "types":["locality","political"]
        },{
          "long_name":"Ohio",
          "short_name":"OH",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
      }
      
      0 lưu trữ dữ liệu bổ sung về vị trí đã chỉ định. Các giá trị sau hiện đang được hỗ trợ
      • {
          "types":["locality","political"],
          "formatted_address":"Toledo, OH, USA",
          "address_components":[{
            "long_name":"Toledo",
            "short_name":"Toledo",
            "types":["locality","political"]
          },{
            "long_name":"Ohio",
            "short_name":"OH",
            "types":["administrative_area_level_1","political"]
          },{
            "long_name":"United States",
            "short_name":"US",
            "types":["country","political"]
          }],
          "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
        }
        
        1 chỉ ra rằng kết quả trả về phản ánh một mã địa lý chính xác
      • {
          "types":["locality","political"],
          "formatted_address":"Toledo, OH, USA",
          "address_components":[{
            "long_name":"Toledo",
            "short_name":"Toledo",
            "types":["locality","political"]
          },{
            "long_name":"Ohio",
            "short_name":"OH",
            "types":["administrative_area_level_1","political"]
          },{
            "long_name":"United States",
            "short_name":"US",
            "types":["country","political"]
          }],
          "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
        }
        
        2 chỉ ra rằng kết quả trả về phản ánh giá trị gần đúng (thường là trên đường) được nội suy giữa hai điểm chính xác (chẳng hạn như giao lộ). Các kết quả được nội suy thường được trả về khi không có mã địa lý trên mái nhà cho một địa chỉ đường phố
      • {
          "types":["locality","political"],
          "formatted_address":"Toledo, OH, USA",
          "address_components":[{
            "long_name":"Toledo",
            "short_name":"Toledo",
            "types":["locality","political"]
          },{
            "long_name":"Ohio",
            "short_name":"OH",
            "types":["administrative_area_level_1","political"]
          },{
            "long_name":"United States",
            "short_name":"US",
            "types":["country","political"]
          }],
          "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
        }
        
        3 chỉ ra rằng kết quả trả về là tâm hình học của kết quả, chẳng hạn như đa tuyến (ví dụ: đường phố) hoặc đa giác (vùng)
      • {
          "types":["locality","political"],
          "formatted_address":"Toledo, OH, USA",
          "address_components":[{
            "long_name":"Toledo",
            "short_name":"Toledo",
            "types":["locality","political"]
          },{
            "long_name":"Ohio",
            "short_name":"OH",
            "types":["administrative_area_level_1","political"]
          },{
            "long_name":"United States",
            "short_name":"US",
            "types":["country","political"]
          }],
          "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
        }
        
        4 chỉ ra rằng kết quả trả về là gần đúng

    • {
        "types":["locality","political"],
        "formatted_address":"Toledo, OH, USA",
        "address_components":[{
          "long_name":"Toledo",
          "short_name":"Toledo",
          "types":["locality","political"]
        },{
          "long_name":"Ohio",
          "short_name":"OH",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
      }
      
      5 lưu trữ chế độ xem được đề xuất cho kết quả trả về
    •   var geocoder;
        var map;
        function initialize() {
          geocoder = new google.maps.Geocoder();
          var latlng = new google.maps.LatLng(-34.397, 150.644);
          var mapOptions = {
            zoom: 8,
            center: latlng
          }
          map = new google.maps.Map(document.getElementById('map'), mapOptions);
        }
      
        function codeAddress() {
          var address = document.getElementById('address').value;
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == 'OK') {
              map.setCenter(results[0].geometry.location);
              var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
              });
            } else {
              alert('Geocode was not successful for the following reason: ' + status);
            }
          });
        }
      
      <body onload="initialize()">
       <div id="map" style="width: 320px; height: 480px;"></div>
        <div>
          <input id="address" type="textbox" value="Sydney, NSW">
          <input type="button" value="Encode" onclick="codeAddress()">
        </div>
      </body>
      
      2 (được trả lại tùy chọn) lưu trữ
      {
        "types":["locality","political"],
        "formatted_address":"Toledo, OH, USA",
        "address_components":[{
          "long_name":"Toledo",
          "short_name":"Toledo",
          "types":["locality","political"]
        },{
          "long_name":"Ohio",
          "short_name":"OH",
          "types":["administrative_area_level_1","political"]
        },{
          "long_name":"United States",
          "short_name":"US",
          "types":["country","political"]
        }],
        "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
      }
      
      7 có thể chứa đầy đủ kết quả được trả về. Lưu ý rằng các giới hạn này có thể không khớp với chế độ xem được đề xuất. (Ví dụ: San Francisco bao gồm Quần đảo Farallon, về mặt kỹ thuật là một phần của thành phố, nhưng sẽ không được trả lại trong chế độ xem. )

Các địa chỉ sẽ được Trình mã hóa địa lý trả về bằng cách sử dụng cài đặt ngôn ngữ ưu tiên của trình duyệt hoặc ngôn ngữ được chỉ định khi tải API JavaScript bằng tham số

{
  "types":["locality","political"],
  "formatted_address":"Toledo, OH, USA",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Ohio",
    "short_name":"OH",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
}
8. (Để biết thêm thông tin, xem Bản địa hóa. )

Các loại địa chỉ và các loại thành phần địa chỉ

Mảng

{
  "types":["locality","political"],
  "formatted_address":"Winnetka, IL, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["locality","political"]
  },{
    "long_name":"Illinois",
    "short_name":"IL",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location":[ -87.7417070, 42.1083080],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
}
1 trong biểu thị loại địa chỉ. Mảng
{
  "types":["locality","political"],
  "formatted_address":"Winnetka, IL, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["locality","political"]
  },{
    "long_name":"Illinois",
    "short_name":"IL",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location":[ -87.7417070, 42.1083080],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
}
1 cũng có thể được trả về trong a để chỉ ra loại của thành phần địa chỉ cụ thể. Địa chỉ được bộ mã hóa địa lý trả về có thể có nhiều loại; . Ví dụ: nhiều thành phố được gắn thẻ loại
{
  "types":["locality","political"],
  "formatted_address":"Toledo, España",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Toledo",
    "short_name":"TO",
    "types":["administrative_area_level_2","political"]
  },{
    "long_name":"Castilla-La Mancha",
    "short_name":"CM",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"España",
    "short_name":"ES",
    "types":["country","political"]
  }],
  "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
}
1 và
{
  "types":["locality","political"],
  "formatted_address":"Toledo, España",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Toledo",
    "short_name":"TO",
    "types":["administrative_area_level_2","political"]
  },{
    "long_name":"Castilla-La Mancha",
    "short_name":"CM",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"España",
    "short_name":"ES",
    "types":["country","political"]
  }],
  "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
}
2

Các loại sau đây được bộ mã hóa địa lý hỗ trợ và trả về ở cả loại địa chỉ và loại thành phần địa chỉ

  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    3 cho biết địa chỉ đường phố chính xác
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    4 cho biết tuyến đường được đặt tên (chẳng hạn như "US 101")
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    5 biểu thị giao lộ chính, thường là của hai con đường lớn
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    1 biểu thị một thực thể chính trị. Thông thường, loại này chỉ ra một đa giác của một số cơ quan hành chính dân sự
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    7 biểu thị thực thể chính trị quốc gia và thường là loại thứ tự cao nhất được trả về bởi Bộ mã hóa địa lý
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    8 biểu thị thực thể dân sự cấp một dưới cấp quốc gia. Trong phạm vi Hoa Kỳ, các cấp hành chính này là các bang. Không phải tất cả các quốc gia thể hiện các cấp hành chính. Trong hầu hết các trường hợp, các tên viết tắt cấp_khu vực_quản trị sẽ khớp chặt chẽ với các phân mục theo tiêu chuẩn ISO 3166-2 và các danh sách được lưu hành rộng rãi khác;
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    9 biểu thị thực thể dân sự cấp hai dưới cấp quốc gia. Tại Hoa Kỳ, các cấp hành chính này là các hạt. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    0 chỉ ra một thực thể dân sự bậc ba dưới cấp quốc gia. Loại này chỉ ra một bộ phận dân sự nhỏ. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    1 chỉ ra một thực thể dân sự cấp bốn dưới cấp quốc gia. Loại này chỉ ra một bộ phận dân sự nhỏ. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    2 chỉ ra một thực thể dân sự bậc năm dưới cấp quốc gia. Loại này chỉ ra một bộ phận dân sự nhỏ. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    3 chỉ ra một thực thể dân sự bậc sáu dưới cấp quốc gia. Loại này chỉ ra một bộ phận dân sự nhỏ. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    4 chỉ ra một thực thể dân sự bậc bảy dưới cấp quốc gia. Loại này chỉ ra một bộ phận dân sự nhỏ. Không phải tất cả các quốc gia thể hiện các cấp hành chính
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    5 chỉ ra một tên thay thế thường được sử dụng cho thực thể
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    2 cho biết một thực thể chính trị thành phố hoặc thị trấn được thành lập
  • function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    7 chỉ ra một thực thể dân sự cấp một bên dưới một địa phương. Đối với một số địa điểm có thể nhận được một trong các loại bổ sung.
    function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    8 đến
    function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    9. Mỗi cấp địa phương là một thực thể dân sự. Số lớn hơn cho thấy một khu vực địa lý nhỏ hơn
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    0 biểu thị một vùng lân cận được đặt tên
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;

    1 cho biết một vị trí được đặt tên, thường là một tòa nhà hoặc tập hợp các tòa nhà có tên chung
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    2 biểu thị một thực thể cấp một bên dưới một vị trí được đặt tên, thường là một tòa nhà đơn lẻ trong một tập hợp các tòa nhà có tên chung
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    3 biểu thị tham chiếu vị trí được mã hóa, bắt nguồn từ kinh độ và vĩ độ. Mã cộng có thể được sử dụng để thay thế cho địa chỉ đường phố ở những nơi không tồn tại (nơi các tòa nhà không được đánh số hoặc đường phố không được đặt tên). Xem https. //thêm. mã để biết chi tiết
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    4 cho biết mã bưu chính được sử dụng để giải quyết thư bưu điện trong nước
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    5 chỉ ra một đặc điểm tự nhiên nổi bật
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    6 biểu thị một sân bay
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    7 chỉ ra một công viên được đặt tên
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    8 chỉ ra một điểm ưa thích được đặt tên. Thông thường, những "POI" này là các thực thể địa phương nổi bật không dễ dàng phù hợp với danh mục khác, chẳng hạn như "Tòa nhà Empire State" hoặc "Tháp Eiffel"

Danh sách các loại trống cho biết không có loại nào được biết đến cho thành phần địa chỉ cụ thể, ví dụ: Lieu-dit ở Pháp

Ngoài những điều trên, các thành phần địa chỉ có thể bao gồm các loại bên dưới

Ghi chú. Danh sách này không đầy đủ và có thể thay đổi

  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    9 cho biết tầng của địa chỉ tòa nhà
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    0 thường chỉ ra một địa điểm chưa được phân loại
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    1 chỉ ra một địa điểm gần đó được sử dụng làm tài liệu tham khảo, để hỗ trợ điều hướng
  • function initMap(): void {
      const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
        {
          zoom: 8,
          center: { lat: 40.731, lng: -73.997 },
        }
      );
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      (document.getElementById("submit") as HTMLElement).addEventListener(
        "click",
        () => {
          geocodeLatLng(geocoder, map, infowindow);
        }
      );
    }
    
    function geocodeLatLng(
      geocoder: google.maps.Geocoder,
      map: google.maps.Map,
      infowindow: google.maps.InfoWindow
    ) {
      const input = (document.getElementById("latlng") as HTMLInputElement).value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    declare global {
      interface Window {
        initMap: () => void;
      }
    }
    window.initMap = initMap;
    8 chỉ ra một điểm ưa thích được đặt tên
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    3 cho biết bãi đậu xe hoặc cấu trúc bãi đậu xe
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;

    4 cho biết một hộp thư cụ thể
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    5 biểu thị một nhóm các khu vực địa lý, chẳng hạn như
    {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    2 và
    function codeAddress() {
    geocoder.geocode({
      componentRestrictions: {
        country: 'AU',
        postalCode: '2000'
      }
    }, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
      } else {
        window.alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    }
    
    7, được sử dụng cho địa chỉ gửi thư ở một số quốc gia
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    8 cho biết phòng của địa chỉ tòa nhà
  • function initMap() {
      const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 8,
        center: { lat: 40.731, lng: -73.997 },
      });
      const geocoder = new google.maps.Geocoder();
      const infowindow = new google.maps.InfoWindow();
    
      document.getElementById("submit").addEventListener("click", () => {
        geocodeLatLng(geocoder, map, infowindow);
      });
    }
    
    function geocodeLatLng(geocoder, map, infowindow) {
      const input = document.getElementById("latlng").value;
      const latlngStr = input.split(",", 2);
      const latlng = {
        lat: parseFloat(latlngStr[0]),
        lng: parseFloat(latlngStr[1]),
      };
    
      geocoder
        .geocode({ location: latlng })
        .then((response) => {
          if (response.results[0]) {
            map.setZoom(11);
    
            const marker = new google.maps.Marker({
              position: latlng,
              map: map,
            });
    
            infowindow.setContent(response.results[0].formatted_address);
            infowindow.open(map, marker);
          } else {
            window.alert("No results found");
          }
        })
        .catch((e) => window.alert("Geocoder failed due to: " + e));
    }
    
    window.initMap = initMap;
    9 cho biết số đường phố chính xác
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    00,
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    01 và
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    02 cho biết vị trí của một trạm dừng xe buýt, xe lửa hoặc phương tiện công cộng

Mã trạng thái

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
9 có thể trả về một trong các giá trị sau

  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    04 cho biết không có lỗi nào xảy ra;
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    05 cho biết rằng mã địa lý đã thành công nhưng không trả về kết quả nào. Điều này có thể xảy ra nếu bộ mã hóa địa lý được thông qua một
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    7 không tồn tại
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    07 cho biết rằng bạn đã vượt quá hạn ngạch của mình
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    08 chỉ ra rằng yêu cầu của bạn đã bị từ chối. Trang web không được phép sử dụng trình mã hóa địa lý
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    09 thường chỉ ra rằng truy vấn (_______0_______7,
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    11 hoặc
    results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    12) bị thiếu
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    13 cho biết rằng không thể xử lý yêu cầu do lỗi máy chủ. Yêu cầu có thể thành công nếu bạn thử lại
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    14 cho biết rằng yêu cầu đã hết thời gian chờ hoặc đã xảy ra sự cố khi liên hệ với máy chủ Google. Yêu cầu có thể thành công nếu bạn thử lại

Trong ví dụ này, chúng tôi mã hóa địa lý một địa chỉ và đặt điểm đánh dấu ở các giá trị kinh độ và vĩ độ được trả về. Lưu ý rằng trình xử lý được chuyển dưới dạng hàm ẩn danh theo nghĩa đen

________số 8_______

Xem ví dụ

Xu hướng khung nhìn

Bạn có thể hướng dẫn Dịch vụ mã hóa địa lý ưu tiên kết quả trong một chế độ xem nhất định (được biểu thị dưới dạng hộp giới hạn). Bạn làm như vậy bằng cách đặt tham số

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
2 trong đối tượng
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
5 để xác định giới hạn của chế độ xem này. Lưu ý rằng xu hướng chỉ thích kết quả trong giới hạn;

Ví dụ: mã địa lý cho "Winnetka" thường trả về vùng ngoại ô này của Chicago

{
  "types":["locality","political"],
  "formatted_address":"Winnetka, IL, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["locality","political"]
  },{
    "long_name":"Illinois",
    "short_name":"IL",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location":[ -87.7417070, 42.1083080],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
}

Tuy nhiên, việc chỉ định tham số

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
2 xác định hộp giới hạn cho Thung lũng San Fernando của Los Angeles dẫn đến mã địa lý này trả về vùng lân cận có tên "Winnetka" ở vị trí đó

{
  "types":["sublocality","political"],
  "formatted_address":"Winnetka, California, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["sublocality","political"]
  },{
    "long_name":"Los Angeles",
    "short_name":"Los Angeles",
    "types":["administrative_area_level_3","political"]
  },{
    "long_name":"Los Angeles",
    "short_name":"Los Angeles",
    "types":["administrative_area_level_2","political"]
  },{
    "long_name":"California",
    "short_name":"CA",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location": [34.213171,-118.571022],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ"
}

Xu hướng mã vùng

Bạn có thể đặt Dịch vụ mã hóa địa lý để trả về kết quả thiên về một khu vực cụ thể một cách rõ ràng bằng cách sử dụng tham số

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
6. Tham số này nhận mã vùng, được chỉ định dưới dạng thẻ phụ vùng Unicode gồm hai ký tự (không phải số). Các thẻ này ánh xạ trực tiếp tới các giá trị hai ký tự ccTLD ("miền cấp cao nhất") quen thuộc, chẳng hạn như "uk" trong "co. Anh" chẳng hạn. Trong một số trường hợp, thẻ
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
6 cũng hỗ trợ mã ISO-3166-1, mã này đôi khi khác với giá trị ccTLD (ví dụ: "GB" cho "Great Britain")

Khi sử dụng tham số

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
6

  • Chỉ xác định một quốc gia hoặc khu vực. Nhiều giá trị bị bỏ qua và có thể dẫn đến yêu cầu không thành công
  • Chỉ sử dụng thẻ phụ vùng gồm hai ký tự (định dạng Unicode CLDR). Tất cả các đầu vào khác sẽ dẫn đến lỗi
  • Chỉ các quốc gia và khu vực được liệt kê trong Chi tiết phạm vi nền tảng Google Maps mới được hỗ trợ

Yêu cầu mã hóa địa lý có thể được gửi cho mọi miền trong đó ứng dụng Google Maps chính cung cấp mã hóa địa lý. Lưu ý rằng xu hướng chỉ thích kết quả cho một miền cụ thể;

Ví dụ: mã địa lý cho "Toledo" trả về kết quả này vì miền mặc định cho Dịch vụ mã hóa địa lý được đặt thành Hoa Kỳ

{
  "types":["locality","political"],
  "formatted_address":"Toledo, OH, USA",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Ohio",
    "short_name":"OH",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "place_id": "ChIJeU4e_C2HO4gRRcM6RZ_IPHw"
}

Mã địa lý cho "Toledo" với trường

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
6 được đặt thành
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
22 (Tây Ban Nha) sẽ trả về thành phố của Tây Ban Nha

{
  "types":["locality","political"],
  "formatted_address":"Toledo, España",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Toledo",
    "short_name":"TO",
    "types":["administrative_area_level_2","political"]
  },{
    "long_name":"Castilla-La Mancha",
    "short_name":"CM",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"España",
    "short_name":"ES",
    "types":["country","political"]
  }],
  "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
}

Lọc thành phần

Bạn có thể đặt Dịch vụ mã hóa địa lý để trả về kết quả địa chỉ được giới hạn ở một khu vực cụ thể bằng cách sử dụng bộ lọc thành phần. Chỉ định bộ lọc trong tham số. Các giá trị bộ lọc hỗ trợ các phương pháp sửa chính tả và đối sánh một phần giống như các yêu cầu mã hóa địa lý khác

Trình mã hóa địa lý chỉ trả về các kết quả khớp với tất cả các bộ lọc thành phần. Nghĩa là, nó đánh giá các thông số kỹ thuật của bộ lọc là AND, không phải OR

Bộ lọc thành phần bao gồm một hoặc nhiều mục sau

  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    4 khớp với tên dài hoặc tên ngắn của tuyến đường
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    2 trận đấu với các loại địa phương và tiểu địa phương
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    26 phù hợp với tất cả các cấp của khu vực hành chính
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    27 khớp với mã bưu chính và tiền tố mã bưu chính
  • {
      "types":["locality","political"],
      "formatted_address":"Toledo, España",
      "address_components":[{
        "long_name":"Toledo",
        "short_name":"Toledo",
        "types":["locality","political"]
      },{
        "long_name":"Toledo",
        "short_name":"TO",
        "types":["administrative_area_level_2","political"]
      },{
        "long_name":"Castilla-La Mancha",
        "short_name":"CM",
        "types":["administrative_area_level_1","political"]
      },{
        "long_name":"España",
        "short_name":"ES",
        "types":["country","political"]
      }],
      "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
    }
    
    7 khớp với tên quốc gia hoặc mã quốc gia gồm hai chữ cái theo tiêu chuẩn ISO 3166-1. Ghi chú. API tuân theo tiêu chuẩn ISO để xác định quốc gia và bộ lọc hoạt động tốt nhất khi sử dụng mã ISO tương ứng của quốc gia

Ví dụ sau minh họa việc sử dụng tham số để lọc theo

{
  "types":["locality","political"],
  "formatted_address":"Toledo, España",
  "address_components":[{
    "long_name":"Toledo",
    "short_name":"Toledo",
    "types":["locality","political"]
  },{
    "long_name":"Toledo",
    "short_name":"TO",
    "types":["administrative_area_level_2","political"]
  },{
    "long_name":"Castilla-La Mancha",
    "short_name":"CM",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"España",
    "short_name":"ES",
    "types":["country","political"]
  }],
  "place_id": "ChIJ8f21C60Lag0R_q11auhbf8Y"
}
7 và
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
27

function codeAddress() {
geocoder.geocode({
  componentRestrictions: {
    country: 'AU',
    postalCode: '2000'
  }
}, function(results, status) {
  if (status == 'OK') {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location
    });
  } else {
    window.alert('Geocode was not successful for the following reason: ' + status);
  }
});
}

Mã hóa địa lý ngược (Tra cứu địa chỉ)

Thuật ngữ mã hóa địa lý thường đề cập đến việc dịch một địa chỉ mà con người có thể đọc được thành một vị trí trên bản đồ. Quá trình thực hiện ngược lại, dịch một vị trí trên bản đồ thành địa chỉ mà con người có thể đọc được, được gọi là mã hóa địa lý ngược

Thay vì cung cấp một văn bản

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
7, hãy cung cấp một cặp kinh độ/vĩ độ được phân tách bằng dấu phẩy trong tham số
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
8

Ghi chú. Nếu bạn bao gồm thông số
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
5 trong yêu cầu thì thông số
results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
8 sẽ bị bỏ qua

Ví dụ sau mã hóa địa lý một giá trị kinh độ/vĩ độ và căn giữa bản đồ tại vị trí đó, hiển thị cửa sổ thông tin với địa chỉ được định dạng

bản đánh máy

function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 8,
      center: { lat: 40.731, lng: -73.997 },
    }
  );
  const geocoder = new google.maps.Geocoder();
  const infowindow = new google.maps.InfoWindow();

  (document.getElementById("submit") as HTMLElement).addEventListener(
    "click",
    () => {
      geocodeLatLng(geocoder, map, infowindow);
    }
  );
}

function geocodeLatLng(
  geocoder: google.maps.Geocoder,
  map: google.maps.Map,
  infowindow: google.maps.InfoWindow
) {
  const input = (document.getElementById("latlng") as HTMLInputElement).value;
  const latlngStr = input.split(",", 2);
  const latlng = {
    lat: parseFloat(latlngStr[0]),
    lng: parseFloat(latlngStr[1]),
  };

  geocoder
    .geocode({ location: latlng })
    .then((response) => {
      if (response.results[0]) {
        map.setZoom(11);

        const marker = new google.maps.Marker({
          position: latlng,
          map: map,
        });

        infowindow.setContent(response.results[0].formatted_address);
        infowindow.open(map, marker);
      } else {
        window.alert("No results found");
      }
    })
    .catch((e) => window.alert("Geocoder failed due to: " + e));
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;
Lưu ý. Đọc hướng dẫn sử dụng TypeScript và Google Maps

JavaScript

function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 8,
    center: { lat: 40.731, lng: -73.997 },
  });
  const geocoder = new google.maps.Geocoder();
  const infowindow = new google.maps.InfoWindow();

  document.getElementById("submit").addEventListener("click", () => {
    geocodeLatLng(geocoder, map, infowindow);
  });
}

function geocodeLatLng(geocoder, map, infowindow) {
  const input = document.getElementById("latlng").value;
  const latlngStr = input.split(",", 2);
  const latlng = {
    lat: parseFloat(latlngStr[0]),
    lng: parseFloat(latlngStr[1]),
  };

  geocoder
    .geocode({ location: latlng })
    .then((response) => {
      if (response.results[0]) {
        map.setZoom(11);

        const marker = new google.maps.Marker({
          position: latlng,
          map: map,
        });

        infowindow.setContent(response.results[0].formatted_address);
        infowindow.open(map, marker);
      } else {
        window.alert("No results found");
      }
    })
    .catch((e) => window.alert("Geocoder failed due to: " + e));
}

window.initMap = initMap;
Ghi chú. JavaScript được biên dịch từ đoạn mã TypeScript

Xem ví dụ

thử mẫu

Stackblitz. com Mã Sandbox. io JSFiddle. mạng Google Cloud Shell

Lưu ý rằng trong ví dụ trước, chúng tôi đã hiển thị kết quả đầu tiên bằng cách chọn

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
36. Trình mã hóa địa lý đảo ngược thường trả về nhiều hơn một kết quả. Địa chỉ được mã hóa địa lý không chỉ là địa chỉ bưu chính, mà là bất kỳ cách nào để đặt tên địa lý cho một vị trí. Ví dụ: khi mã hóa địa lý một điểm trong thành phố Chicago, điểm được mã hóa địa lý có thể được gắn nhãn là địa chỉ đường phố, thành phố (Chicago), tiểu bang của nó (Illinois) hoặc quốc gia (Hoa Kỳ). Tất cả đều là địa chỉ cho bộ mã hóa địa lý. Trình mã hóa địa lý ngược trả về tất cả các kết quả này

Trình mã hóa địa lý ngược khớp với các thực thể chính trị (quốc gia, tỉnh, thành phố và vùng lân cận), địa chỉ đường phố và mã bưu chính

Đây là một ví dụ về danh sách các địa chỉ mà truy vấn trên có thể trả về

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
0

Địa chỉ được trả về theo thứ tự phù hợp nhất đến ít nhất. Nói chung, địa chỉ chính xác hơn là kết quả nổi bật nhất, như trong trường hợp này. Lưu ý rằng chúng tôi trả về các loại địa chỉ khác nhau, từ địa chỉ đường phố cụ thể nhất đến các thực thể chính trị ít cụ thể hơn như vùng lân cận, thành phố, hạt, tiểu bang, v.v. Nếu bạn muốn khớp một địa chỉ tổng quát hơn, bạn có thể muốn kiểm tra trường

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   postcode_localities[]: string,
   types[]: string
 },
 partial_match: boolean,
 place_id: string,
 postcode_localities[]: string,
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}
37

Ghi chú. Mã hóa địa lý ngược không phải là một khoa học chính xác. Trình mã hóa địa lý sẽ cố gắng tìm vị trí có thể định địa chỉ gần nhất trong một dung sai nhất định

Truy xuất Địa chỉ cho ID Địa điểm

Cung cấp một

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
1 để tìm địa chỉ cho một ID địa điểm nhất định. ID địa điểm là một mã định danh duy nhất có thể được sử dụng với các API Google khác. Ví dụ: bạn có thể cung cấp
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
1 do API Đường bộ trả về để nhận địa chỉ của một điểm bị gãy. Để biết thêm thông tin về ID địa điểm, hãy xem tổng quan về ID địa điểm

Khi bạn cung cấp

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng
    }
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == 'OK') {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

<body onload="initialize()">
 <div id="map" style="width: 320px; height: 480px;"></div>
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>
1, yêu cầu không được chứa bất kỳ trường nào sau đây

  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    7
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    42
  • results[]: {
     types[]: string,
     formatted_address: string,
     address_components[]: {
       short_name: string,
       long_name: string,
       postcode_localities[]: string,
       types[]: string
     },
     partial_match: boolean,
     place_id: string,
     postcode_localities[]: string,
     geometry: {
       location: LatLng,
       location_type: GeocoderLocationType
       viewport: LatLngBounds,
       bounds: LatLngBounds
     }
    }
    
    8
  •   var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 8,
          center: latlng
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
      }
    
      function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    
    <body onload="initialize()">
     <div id="map" style="width: 320px; height: 480px;"></div>
      <div>
        <input id="address" type="textbox" value="Sydney, NSW">
        <input type="button" value="Encode" onclick="codeAddress()">
      </div>
    </body>
    
    5

Ví dụ sau chấp nhận ID địa điểm, tìm địa chỉ tương ứng và căn giữa bản đồ tại vị trí đó. Nó cũng hiển thị một cửa sổ thông tin hiển thị địa chỉ được định dạng của địa điểm có liên quan