去最近的修理厂 (最近的修理店在哪里)

杭州洗浴 04-17 阅读:45 评论:0
javascript // 初始化地图 var map = new google.maps.Map(document.getElementById('map'), {center: {lat: 40.7128, lng: -74.0059},zoom: 12 });// 创建信息窗口 var infowindow = new google.maps.InfoWindow();// 获取当前位置 if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(function(position) {var pos = {lat: position.coords.latitude,lng: position.coords.longitude};// 在当前位置放置标记var marker = new google.maps.Marker({position: pos,map: map});// 设置信息窗口内容infowindow.setContent('你在这里');infowindow.open(map, marker);// 搜索附近的修理厂var request = {location: pos,radius: 5000,type: ['car_repair']};var service = new google.maps.places.PlacesService(map);service.nearbySearch(request, function(results, status) {if (status === google.maps.places.PlacesServiceStatus.OK) {// 在地图上显示搜索结果for (var i = 0; i < results.length; i++) {var place = results[i];var marker = new google.maps.Marker({position: place.geometry.location,map: map});// 设置信息窗口内容var content = '
' + place.name + ' 去最近的修理厂 (最近的修理店在哪里)
' +place.vicinity + '
';google.maps.event.addListener(marker, 'click', function() {infowindow.setContent(content);infowindow.open(map, marker);});}}});}, function() {// 处理定位错误handleLocationError(true, infowindow, map.getCenter());}); } else {// 处理浏览器不支持地理定位的情况handleLocationError(false, infowindow, map.getCenter()); }// 处理定位错误 function handleLocationError(browserHasGeolocation, infoWindow, pos) {infoWindow.setPosition(pos);infoWindow.setContent(browserHasGeolocation ?'Error: 定位超时' :'Error: 浏览器不支持地理定位');infoWindow.open(map); }CSS css body {font-family: Arial, sans-serif; }h1 {text-align: center; }map {width: 100%;height: 600px; }.infowindow-content {font-size: 14px;line-height: 1.4;padding: 12px; }
版权声明

本文仅代表作者观点,不代表杭州桑拿立场。
本文系作者授权发表,未经许可,不得转载。