2016-08-08 5 views
0

나는이 위치에서 나의 위치를보고 있으며, 동시에 json 배열로부터 여러 위치를 표시하고 싶지만지도에 표시 할뿐만 아니라 볼 필요가있다. 방화범지구 위치 watchposition 여러 위치?

function initMap(userId,locations) { 
    var directionsService = new google.maps.DirectionsService; 
    var directionsDisplay; 
    var icn="img/mechanic.png"; 
    var meIcn="img/me.png"; 

    directionsDisplay = new google.maps.DirectionsRenderer({ 
    polylineOptions: { 
    strokeColor: "red" 
    } 
}); 

var map = new google.maps.Map(document.getElementById('map-canvas'), { 
zoom: 13, 
center: new google.maps.LatLng(53.529773,-113.509387), //ALBERTA 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

directionsDisplay.setMap(map); 

var marker, i; 
var infowindow = new google.maps.InfoWindow(); 

$.each(locations, function(index, element) { 
watchMec = navigator.geolocation.watchPosition(function(position){ 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(
      parseFloat(element.longitud), parseFloat(element.latitud)), 
     map: map, 
     icon: icn 
    }); 
}); 

google.maps.event.addListener(marker, 'click', (function(marker, i) { 
       // My position 
    watchId = navigator.geolocation.watchPosition(function(position){ 
    lat = position.coords["latitude"]; 
lng = position.coords["longitude"]; 
markerMe = new google.maps.Marker({position: {lat: lat, lng: lng}, 
    map:map, icon:meIcn});    
    }); 

    return function() { 
    var distinationOrigin = new google.maps.LatLng(lat, lng); //My position 
    var destinationMarker = parseFloat(element.longitud) + ',' + '' + 
    parseFloat(element.latitud); 
    infowindow.setContent(element.datos); 
    infowindow.open(map, marker); 
    calculateAndDisplayRoute(directionsService, directionsDisplay, 
    distinationOrigin, destinationMarker, infowindow); 
    } 
    })(marker, i)); 
}) 
} 

가 (모질라) 나는 형식 오류를 얻을 수 있지만 정확히 어디에 모른다 : 나는 지금까지이 있어요.

이 경우 어떻게 watchposition을 사용할 수 있습니까? 나는 그것을 해결 가지고 사전

+1

"_I typeError가 발생하지만 어디에서 정확하게 나온 것입니까?" Firebug는 오류 (파일 이름, 행 및 열)의 정확한 위치를 언급합니다. 오류가 발생한 위치로 건너 뛰려면 클릭하십시오. – Andreas

+0

그리고 그것은 정확한 오류 메시지도 언급합니다, btw. – GolezTrol

+0

예, 좋습니다. 오류의 정확한 위치는 다음과 같습니다. ... turnValue = "처리")}; mb = function (a, b) {a .__ e3_ || (a .__ e3_ {}); a = a .__ e3_ ; a [b] || (a [... 정확한 오류 메시지는 다음과 같습니다. TypeError : a는 정의되지 않음 파일은 https://maps.googleapis.com/maps/api/js에 있습니다. libraries = geometry, places & key = xxx 도움이 필요하십니까? –

답변

0

덕분에, 문제는 내가 마커 객체에서 리스너 이벤트를 가하고, 그래서 마커 변수가 올바르게 구문 분석되지 않은 것이 었습니다. 해결책은 다음과 같습니다.

$.each(locations, function(index, element) { 
watchMec = navigator.geolocation.watchPosition(function(position){ 
    marker = new google.maps.Marker({ 
     position: {lat:parseFloat(element.longitud), lng:parseFloat 
      (element.latitud)}, 
     map: map, 
     icon:icn 
    }); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     // My position 
     watchId = navigator.geolocation.watchPosition(function(position){ 
      lat = position.coords["latitude"]; 
      lng = position.coords["longitude"]; 
      markerMe = new google.maps.Marker({position: {lat: lat, lng: lng}, map: map, icon:meIcn});    
     }); 

     return function() { 
      var distinationOrigin = new google.maps.LatLng(lat, lng); // MI POSICION 
      var destinationMarker = parseFloat(element.longitud) + ',' + '' + parseFloat(element.latitud); 
      infowindow.setContent(element.datos); 
      infowindow.open(map, marker); 
      calculateAndDisplayRoute(directionsService, directionsDisplay, distinationOrigin, destinationMarker, infowindow); 
     } 
    })(marker, i));  
}); 
});