【问题标题】:Display location Using Longitude/Latitude Coordinates - Google Maps使用经度/纬度坐标显示位置 - Google 地图
【发布时间】:2011-10-31 10:23:10
【问题描述】:

如何输入一组坐标并让 Google 地图显示位置。这一切都需要通过 javascript 来完成(即:没有用户界面)

感谢任何帮助

【问题讨论】:

  • 我也问过同样的问题,他们回答了 --> stackoverflow.com/questions/8766116/…
  • @SamekaTV - 该链接是 OPPOSITE 目标(单击地图,然后返回经度/纬度)。这个问题是给定一个已知的经度/纬度,如何显示该位置的地图。 (或者也许 shahmeer 正在询问相关问题,即如何在已经打开的地图上显示该位置的标记。)

标签: javascript google-maps coordinates


【解决方案1】:

注意:如果您已经有了纬度/经度,请参阅Alteveer’s answer

您需要使用地理定位服务来获取纬度/经度。谷歌地图有一个内置的: http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

这是一个如何使用它的示例:

<!-- Placeholder for the Google Map -->
<div id="map" style="height: 512px;">
  <noscript>
    <!-- http://code.google.com/apis/maps/documentation/staticmaps/ -->
    <img src="http://maps.google.com/maps/api/staticmap?center=1%20infinite%20loop%20cupertino%20ca%2095014&amp;zoom=16&amp;size=512x512&amp;maptype=roadmap&amp;sensor=false" />
  </noscript>
</div>

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

// Define the address we want to map.
var address = "1 infinite loop cupertino ca 95014";

// Create a new Geocoder
var geocoder = new google.maps.Geocoder();

// Locate the address using the Geocoder.
geocoder.geocode( { "address": address }, function(results, status) {

  // If the Geocoding was successful
  if (status == google.maps.GeocoderStatus.OK) {

    // Create a Google Map at the latitude/longitude returned by the Geocoder.
    var myOptions = {
      zoom: 16,
      center: results[0].geometry.location,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    // Add a marker at the address.
    var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location
    });

  } else {
    try {
      console.error("Geocode was not successful for the following reason: " + status);
    } catch(e) {}
  }
});
</script>

【讨论】:

  • 这回答了一个不同的问题。提出的问题是:给定纬度/经度,显示地图或地图上的标记。这个答案是:给定一个地址,在地图上显示一个标记。 如果已经有纬度/经度,则查看// Add a marker at the address. 下的行以显示标记。或者,如果要打开新地图,请参阅Alteveer's answer
  • 这很好,史蒂夫。 (糟糕!)我在这个答案中添加了注释。
【解决方案2】:

您应该能够使用纬度/经度初始化地图,如下所示:https://developers.google.com/maps/documentation/javascript/tutorial#MapOptions

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    相关资源
    最近更新 更多