显示位置使用经度/纬度坐标 - Google地图

时间:2011-08-21 01:10:19

标签: javascript google-maps coordinates

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

来完成的

感谢任何帮助

2 个答案:

答案 0 :(得分:11)

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

您需要使用地理定位服务来获取纬度/经度。 Google地图内置了一个: 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>

答案 1 :(得分:2)

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