2016-09-27 45 views
0

我正在嘗試創建一個地圖,顯示您當前的位置並在城市中顯示標記。所以你可以看看你是否在附近的一個標記。使用Google Maps Javascript結合地理位置和標記

我必須在這個時候編碼,我需要將它們合併。但是,當我這樣做時,代碼不再工作。有人知道如何將這兩個JS結合起來嗎?如果沒有,還有其他方法可以做到這一點嗎?

這是JS的地理位置:

function initMap() 
var map = new google.maps.Map(document.getElementById('map'), { 
center: {lat: -34.397, lng: 150.644}, 
zoom: 14, 
}); 
var infoWindow = new google.maps.InfoWindow({map: map}); 
if (navigator.geolocation) { 
navigator.geolocation.getCurrentPosition(function(position) { 
    var pos = { 
    lat: position.coords.latitude, 
    lng: position.coords.longitude 
    }; 

    infoWindow.setPosition(pos); 
    infoWindow.setContent('Location found.'); 
    map.setCenter(pos); 
}, function() { 
    handleLocationError(true, infoWindow, map.getCenter()); 
}); 
} else { 
// Browser doesn't support Geolocation 
handleLocationError(false, infoWindow, map.getCenter()); 
} 
} 
function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
infoWindow.setPosition(pos); 
infoWindow.setContent(browserHasGeolocation ? 
        'Error: The Geolocation service failed.' : 
        'Error: Your browser doesn\'t support geolocation.'); 
} 

這是JS代碼我已經爲標記製作:

google.maps.event.addDomListener(window, 'load', initialize); 
var map; 
var currentPage = 'mapA'; 
var markers = [ 
['Thuis',52.2203713,6.90032999999994], 
['Bommelstein',52.22254299999999,6.911623800000029], 
['Saxion', 52.21976009999999,6.889364500000056], 
['Kroeg', 52.2206085,6.896547300000066], 
['t Gat', 52.2211657,6.895848999999998], 
['studieruimte de Bul', 52.22101940903001,6.8924690438436755] 
]; 
function initialize() { 
var mapOptions = { 
    zoom: 14, 
    disableDoubleClickZoom: true, 
    draggable: true, 
    panControl: false, 
    scrollwheel: true, 
    zoomControl: true, 
    streetViewControl: false, 
    overviewMapControl: false, 
    mapTypeControl: false, 
    center: new google.maps.LatLng(51.489031, 7.039671) 
}; 
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions); 

setMarkers();}function setMarkers(){ 
for (i = 0; i < markers.length; i++) { 
    var post = new google.maps.LatLng(markers[i][1], markers[i][2]); 
    var marker = new google.maps.Marker({ 
     position: post, 
     map: map 
    }); 

    var infowindow = new google.maps.InfoWindow({ 
     content: markers[i][0] 
    }); 

    infowindow.open(map,marker); 
} 
} 
+0

可能重複[如何使用javascript地理位置和多指標結合起來?](http://stackoverflow.com/questions/10932496/如何對結合,地理位置和 - 多標記,使用的JavaScript) – geocodezip

回答

0

其實我工作的samething了同樣的問題,直到幾分鐘前。在聲明地圖對象之前,我只需啓動地理位置條件來查找位置,並且它對我很有用。這是我與你相似的簡單代碼:

function initMap() { 
    // Try HTML5 geolocation. 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      var currentpos = { 
       lat: position.coords.latitude, 
       lng: position.coords.longitude 
      }; 
      var map = new google.maps.Map(document.getElementById('map'), { 
       center: currentpos, 
       zoom: 14, 
      }); 

      var contentString = 'I found you'; 
      var infowindow = new google.maps.InfoWindow({ 
       content: contentString 
      }); 

      var marker = new google.maps.Marker({ 
       position: pos, 
       map: map, 
       title: 'marker with infoWindow' 
      }); 
      marker.addListener('click', function() { 
       infowindow.open(map, marker); 
      }); 
     }, function() { 
      handleLocationError(true, infoWindow, map.getCenter()); 
     }); 
    } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
    } 
} 
function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
    infoWindow.setPosition(pos); 
    infoWindow.setContent(browserHasGeolocation ? 
          'Error: The Geolocation service failed.' : 
          'Error: Your browser doesn\'t support geolocation.'); 
} 

最好的運氣

相關問題