Javascript 使用经度/纬度坐标显示位置 - Google 地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7135722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display location Using Longitude/Latitude Coordinates - Google Maps
提问by shahmeer navid
How can I input a set of coordinates and have Google maps show a location. This is all to be done through javascript (ie: no user interface)
如何输入一组坐标并让 Google 地图显示位置。这一切都可以通过 javascript 完成(即:没有用户界面)
Any help is appreciated
任何帮助表示赞赏
采纳答案by Jim
Note: If you already have a latitude/longitude, see Alteveer's answer.
注意:如果您已经有了纬度/经度,请参阅Alteveer 的回答。
You'll need to need to use a geolocation service to get the latitude/longitude. Google Maps has one built in: http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
您需要使用地理定位服务来获取纬度/经度。谷歌地图有一个内置的:http: //code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
Here's an example of how to use it:
以下是如何使用它的示例:
<!-- 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&zoom=16&size=512x512&maptype=roadmap&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>
回答by alteveer
You should be able to initialize the map with latitude/longitude as follows: https://developers.google.com/maps/documentation/javascript/tutorial#MapOptions
您应该能够使用纬度/经度初始化地图,如下所示:https: //developers.google.com/maps/documentation/javascript/tutorial#MapOptions