[英]How to get the user's location from iPhone using Google Maps API v3?
I want to make a google map on the iPhone and show the user's location when they first open the site. 我想在iPhone上制作一个Google地图,并在用户首次打开该网站时显示其位置。
But i can't find this method on Google Maps v3 api. 但是我在Google Maps v3 api上找不到此方法。 So i think maybe the iPhone has the function to do this.
因此,我认为也许iPhone具有执行此操作的功能。 Does it?
可以?
You may want to use the W3C Geolocation API , which Safari on the iPhone supports. 您可能要使用iPhone上的Safari支持的W3C Geolocation API 。
Plotting a point on Google Maps using the position from the Geolocation API, will look something like this: 使用Geolocation API中的位置在Google Maps上绘制点将看起来像这样:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var point = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: point,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Place a marker
new google.maps.Marker({
position: point,
map: map
});
});
}
else {
alert('W3C Geolocation API is not available');
}
Make sure that the Google Maps API v3 is included in your web document: 确保您的网络文档中包含Google Maps API v3:
<script src="http://maps.google.com/maps/api/js?sensor=true"
type="text/javascript"></script>
... and that you have a placeholder for the map canvas: ...并且您有一个占位符用于地图画布:
<div id="map" style="width: 500px; height: 400px;"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.