javascript – Google地图 – 用户地理位置的说明

前端之家收集整理的这篇文章主要介绍了javascript – Google地图 – 用户地理位置的说明前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下功能,检查浏览器是否支持地理位置,然后获取用户地理位置并将其居中在地图上.

我需要添加什么才能让用户用户地理位置向用户指定固定位置(这不会改变)?

  1. if (navigator.geolocation)
  2. {
  3. navigator.geolocation.getCurrentPosition(function(position)
  4. {
  5. var latitude = position.coords.latitude;
  6. var longitude = position.coords.longitude;
  7. var coords = new google.maps.LatLng(latitude,longitude);
  8. var directionsDisplay;
  9. var directionsService = new google.maps.DirectionsService();
  10. var mapOptions =
  11. {
  12. zoom: 15,center: coords,mapTypeControl: true,navigationControlOptions:
  13. {
  14. style: google.maps.NavigationControlStyle.SMALL
  15. },mapTypeId: google.maps.MapTypeId.ROADMAP
  16. };
  17. map = new google.maps.Map(document.getElementById("mapContainer"),mapOptions);
  18. var marker = new google.maps.Marker(
  19. {
  20. position: coords,map: map,});
  21. });
  22. }
  23. else
  24. {
  25. alert("Geolocation API is not supported in your browser.");

我已将此函数添加到我的代码中:

  1. function calcRoute() {
  2. var start = position;
  3. var end = "London";
  4. var request = {
  5. origin: start,destination: end,travelMode: google.maps.TravelMode.DRIVING
  6. };
  7. directionsService.route(request,function (result,status) {
  8. if (status == google.maps.DirectionsStatus.OK) {
  9. directionsDisplay.setDirections(result);
  10. }
  11. });
  12. }

HTML:
< div id =“mapContainer”onload =“calcRoute()”>< / div>

但它似乎仍然没有奏效.

解决方法

使用以下代码
  1. if (navigator.geolocation) { //Checks if browser supports geolocation
  2. navigator.geolocation.getCurrentPosition(function (position) { //This gets the
  3. var latitude = position.coords.latitude; //users current
  4. var longitude = position.coords.longitude; //location
  5. var coords = new google.maps.LatLng(latitude,longitude); //Creates variable for map coordinates
  6. var directionsService = new google.maps.DirectionsService();
  7. var directionsDisplay = new google.maps.DirectionsRenderer();
  8. var mapOptions = //Sets map options
  9. {
  10. zoom: 15,//Sets zoom level (0-21)
  11. center: coords,//zoom in on users location
  12. mapTypeControl: true,//allows you to select map type eg. map or satellite
  13. navigationControlOptions:
  14. {
  15. style: google.maps.NavigationControlStyle.SMALL //sets map controls size eg. zoom
  16. },mapTypeId: google.maps.MapTypeId.ROADMAP //sets type of map Options:ROADMAP,SATELLITE,HYBRID,TERRIAN
  17. };
  18. map = new google.maps.Map( /*creates Map variable*/ document.getElementById("map"),mapOptions /*Creates a new map using the passed optional parameters in the mapOptions parameter.*/);
  19. directionsDisplay.setMap(map);
  20. directionsDisplay.setPanel(document.getElementById('panel'));
  21. var request = {
  22. origin: coords,destination: 'BT42 1FL',travelMode: google.maps.DirectionsTravelMode.DRIVING
  23. };
  24.  
  25. directionsService.route(request,function (response,status) {
  26. if (status == google.maps.DirectionsStatus.OK) {
  27. directionsDisplay.setDirections(response);
  28. }
  29. });
  30. });
  31. }

猜你在找的JavaScript相关文章