当前位置: 首页 > 知识库问答 >
问题:

如何在谷歌地图中使用地址而不是经度和纬度?[重复]

潘安平
2023-03-14

我有一个正在运行的脚本。我可以拖动标记,它将用经度和纬度填充两个文本输入字段。我有一个变量“address”,它包含州和县。我应该如何修改代码,使其脱离地址变量而不是静态纬度和经度变量。所以最初我希望地图位于地址位置的中间。

function selectState(state_id){
if(state_id!="-1"){
loadData('city',state_id);
var e = document.getElementById("state");
var stateloc = e.options[e.selectedIndex].value;

var address = state_id + stateloc;

 var $latitude = document.getElementById('latitude');
 var $longitude = document.getElementById('longitude');
 var latitude = 35.00636021320537
 var longitude = -53.46687316894531;
 var zoom = 12;

var LatLng = new google.maps.LatLng(latitude, longitude);

var mapOptions = {
  zoom: zoom,
  center: LatLng,
  panControl: false,
  zoomControl: true,
  scaleControl: true,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}  

var map = new google.maps.Map(document.getElementById('map'),mapOptions);

var marker = new google.maps.Marker({
  position: LatLng,
  map: map,
  title: 'Drag Me!',
  draggable: true
});

google.maps.event.addListener(marker, 'dragend', function(marker){
  var latLng = marker.latLng;
  $latitude.value = latLng.lat();
  $longitude.value = latLng.lng();
});


      }else{ $("#city_dropdown").html("<option value='-1'>Select city</option>");
}
  }

html格式

     <li>
              <label for="lat">Latitude:</label>
              <input id="latitude" placeholder="latitude" name="lat" type="text" class="text" />
            </li>
                  <li>
              <label for="lon">Longitude:</label>
              <input id="longitude" placeholder="longitude" name="lon" class="text" type="text"/>
            </li>
   <div id="map" style="width: 460px; height: 350px;"></div> 

共有1个答案

严宏朗
2023-03-14

在此处查看此代码,它使用“加利福尼亚州圣地亚哥”来开始地图。

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var geocoder;
  var map;
  var address ="San Diego, CA";
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
    mapTypeControl: true,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    navigationControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
          map.setCenter(results[0].geometry.location);

            var infowindow = new google.maps.InfoWindow(
                { content: '<b>'+address+'</b>',
                  size: new google.maps.Size(150,50)
                });

            var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map, 
                title:address
            }); 
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map,marker);
            });

          } else {
            alert("No results found");
          }
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
 <div id="map_canvas" style="width:100%; height:100%">
</body>
</html>

答案来源:使用谷歌地图API使用地址代替经度和纬度

确切地说,您需要做的是,使用地理编码将“文本地址”转换为各自的纬度和经度,然后使用这些值来提供 Google 地图 API。

您可以在地图文档中阅读更多关于Google地理编码的信息https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests

以及文档中的示例代码

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Geocoding service</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
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-canvas'), mapOptions);
}

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.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);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="panel">
      <input id="address" type="textbox" value="Sydney, NSW">
      <input type="button" value="Geocode" onclick="codeAddress()">
    </div>
    <div id="map-canvas"></div>
  </body>
</html>
 类似资料:
  • 长话短说,我想更新地图上的标记仅使用传感器数据,但我在API中找不到允许我更新地图上的纬度/经度的库函数。根据我计算的新纬度/经度,有什么东西可以做到这一点吗?

  • 问题内容: 我听说可以提交一个地址而不是经度和纬度,这对于我的系统来说将更加可行。用户在创建个人资料时必须输入他们的地址,之后他们的个人资料将显示其房屋的Google地图。我目前可以将Google Maps API与经度和纬度完美配合使用: 请有人帮我修改代码,以便它使Google Maps API可以在其位置请求地址,因为我想直接从mySQL数据库中读取用户的地址。 问题答案: 使用Google

  • 我正在尝试将Google Places API集成到我的应用程序中。现在我正在查找手机的当前位置,如何将获得的经度和纬度嵌入以下URL,而不是“location=34.0522222,-118.2427778” "https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778

  • 本文向大家介绍使用百度地图api实现根据地址查询经纬度,包括了使用百度地图api实现根据地址查询经纬度的使用技巧和注意事项,需要的朋友参考一下 以上就是代码的全部内容了,小伙伴们可以直接使用在项目中哦,不用跟我说谢谢,请叫我雷锋大大~

  • 谷歌提供了一个静态地图平铺服务。这些是那些256x256静态映射块。它们是否提供了一个将纬度、经度映射到像素坐标的库,反之亦然?我在web上看到了一些这方面的实现,但不知道他们是否有一个官方的实现可以使用。 谢谢

  • 问题内容: 给定纬度和经度,我们如何使用Javascript或Python将其转换为街道地址? 问题答案: 大多数人在99%的时间内将您链接到Google Maps的API。不错的答案。但是- 注意禁止用途,使用限制和使用条款!尽管许多分布式应用程序并未违反使用限制,但对于Web应用程序而言却是相当有限的。TOS不允许您将皮肤上的数据重新用于Google应用程序中。您不希望因Google发出的终止