Google maps 中心地图到我的位置
我有以下代码:Google maps 中心地图到我的位置,google-maps,Google Maps,我有以下代码: map = new google.maps.Map(document.getElementById('map-canvas'), { zoom: 13, center: new google.maps.LatLng(53.529773,-113.509387), mapTypeId: google.maps.MapTypeId.ROADMAP }); 我如何替换: center:new google.maps.LatLng(53.529773,-113
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 13,
center: new google.maps.LatLng(53.529773,-113.509387),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
我如何替换:
center:new google.maps.LatLng(53.529773,-113.509387)
对于我的当前位置,从编程角度看,您的代码看起来是正确的,我怀疑您的问题是您的设备没有启用位置服务,如果启用了,您可能在第一次运行时没有接受安全提示 下面的演示是我根据GoogleMapsAPI参考修改的 HTML演示
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
JS
看看。可能是重复的Ok,也许我没有正确解释,我需要的是替换:
center:new google.maps.LatLng(53.529773,-113.509387)
和我的当前位置,按程序,阅读另一个答案。它描述了如何使用地理定位API,以及如果用户不允许访问该API,该怎么办。关于用户界面部分有一个很好的讨论。如果检索到当前坐标,则将其用于LatLng
对象。如果你不知道怎么做,请阅读关于JavaScript
的教程。这不是代码编写服务。你说的“你当前的位置”是什么意思?动态的?一个具体的地址?好吧,也许我没有正确解释,我需要的是替换:center:new google.maps.LatLng(53.529773,-113.509387)
和我的当前位置,程序上你说的“你的当前位置”是什么意思?嗯,我不知所措,这正是你的代码在做的:)var myCurrentLocation=new google.maps.LatLng(position.coords.lation,position.coords.longitude);map.setCenter(myCurrentLocation);
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
function initMap() {
// Pick a start point, I live in Australia, so just picked the middle of OZ
var myLatLng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
// Mark it on the map, this is the original center
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
// If location services is enabled, the following should center the map on your location.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(location);
});
}
}