2011-08-21 254 views
13

如何输入一组坐标并让Google地图显示位置。这是所有通过JavaScript来实现:显示位置使用经度/纬度坐标 - Google地图

任何帮助表示赞赏(即没有用户界面)

+0

我问过相同的,他们回答 - > http://stackoverflow.com/questions/ 8766116 /如何得到xy数字在谷歌地图上点击每次点击 – SamekaTV

+0

@SamekaTV - 该链接是OPPOSITE的目标(点击地图,获取经度/纬度)。这个问题是一个已知的经度/纬度,如何显示该位置的地图。 (或者也许shahmeer问了相关问题,如何在已经打开的地图上显示该位置的标记。) – ToolmakerSteve

+0

详细的博客:http://sforsuresh.in/display-google-map-locations-使用纬度经度/ –

回答

11

注:如果您已经有经度/纬度,请参阅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> 
+1

这回答了与被问到的不同的问题。问的问题是:给定LATITUDE/LONGITUDE,在地图上显示地图或标记。这个答案是:给定一个地址,在地图上显示一个标记。 **如果已经有lat/long **,那么看到'//在地址上添加一个标记'下的行以显示一个标记。或者如果想要打开一张新地图,请参阅[Alteveer的回答](https://stackoverflow.com/a/7135770/199364)。 – ToolmakerSteve

+0

史蒂夫,这很棒。 (糟糕!)我给这个答案添加了一个注释。 – Jim