2014-04-05 70 views
0

我需要從我的位置開始實施所有關閉藥房的搜索,我寫的代碼有時可以工作,有時候不會,搜索始終有效,但始終有效的是固定我的位置並將其設置爲起點搜索。Google Map API v3搜索從我的位置開始的地點?

有時,當我進入頁面的拳頭一切正常,然後刷新頁面後,經緯度和長度取我設置的默認值,而不是我的位置,當發生這種情況時,我打開調試鉻和追蹤一些線路,突然一切正常,我不知道這裏真的發生了什麼,但希望有人能幫助我,因爲我被困住了。

這裏是我的代碼:

<script src="Scripts/jquery-1.11.0.min.js" type="text/javascript"></script> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&libraries=places"></script> 
<script> 
    var map; 
    var infowindow; 
    var lat = 26.304295; //29.331647; 
    var lng = 50.155233; //48.074473; 

    function success(position) { 

    }; 

    function error(msg) { 
     alert(msg); 
    } 

    var request = $.ajax({ 
     async: false, 
     success: function (data) { 
      navigator.geolocation.getCurrentPosition(function (pos) { 
       lat = pos.coords.latitude; 
       lng = pos.coords.longitude; 

       $("#Lat").val(lat); 
       $("#Lng").val(lng); 
      }, function (error) { 
       // ... 
      }, { timeout: 10000 }); 
     } 
    }); 

    $.when(request).done(function() { 

     function initialize() { 

      if ($("#Lat").val() != "") 
       lat = $("#Lat").val(); 

      if ($("#Lng").val() != "") 
       lng = $("#Lng").val(); 

      var pyrmont = new google.maps.LatLng(lat, lng); 

      map = new google.maps.Map(document.getElementById('map-canvas'), { 
       center: pyrmont, 
       zoom: 14 
      }); 

      var request = { 
       location: pyrmont, 
       radius: 3000, 
       types: ['pharmacy'] 
      }; 
      infowindow = new google.maps.InfoWindow(); 
      var service = new google.maps.places.PlacesService(map); 
      service.nearbySearch(request, callback); 

      var myloc = new google.maps.Marker({ 
       clickable: false, 
       icon: new google.maps.MarkerImage('//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png', 
                 new google.maps.Size(22, 22), 
                 new google.maps.Point(0, 18), 
                 new google.maps.Point(11, 11)), 
       shadow: null, 
       zIndex: 999, 
       map: map 
      }); 

      var me = new google.maps.LatLng(lat, lng); 
      myloc.setPosition(me); 
     } 

     function callback(results, status) { 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
       for (var i = 0; i < results.length; i++) { 
        createMarker(results[i]); 
       } 
      } 
     } 

     function createMarker(place) { 
      var placeLoc = place.geometry.location; 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: place.geometry.location 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.setContent(place.name); 
       infowindow.open(map, this); 
      }); 
     } 

     google.maps.event.addDomListener(window, 'load', initialize); 
    }); 

</script> 

這裏是我的html代碼:

<div id="map-canvas"> 
</div> 
<asp:HiddenField runat="server" ClientIDMode="Static" ID="Lat" Value="" /> 
<asp:HiddenField runat="server" ClientIDMode="Static" ID="Lng" Value="" /> 

回答

0

我想通了什麼是這裏的問題。爲了能夠獲得你的位置,你將不得不首先有一個完全初始化的地圖,這就是爲什麼每當我試圖獲得我的位置時,它總是執行第二個任何我做的事情。爲了解決這個問題,請調用獲取我的位置函數然後初始化地圖,地圖將被初始化,然後我的位置函數將被調用,之後,只需將新的long和lat指向地圖,並搜索地點,那就是。

下面是代碼是如何橫空出世:

<script> 
    var map; 
    var infowindow; 
    var pyrmont; 
    var lat = 29.331647; 
    var lng = 48.074473; 

    function initialize() { 

     var pyrmont = new google.maps.LatLng(lat, lng); 

     map = new google.maps.Map(document.getElementById('map-canvas'), { 
      center: pyrmont, 
      zoom: 14 
     }); 
    } 

    function callback(results, status) { 
     if (status == google.maps.places.PlacesServiceStatus.OK) { 
      for (var i = 0; i < results.length; i++) { 
       createMarker(results[i]); 
      } 
     } 
    } 

    function createMarker(place) { 
     var placeLoc = place.geometry.location; 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: place.geometry.location 
     }); 

     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.setContent(place.name); 
      infowindow.open(map, this); 
     }); 
    } 

    //Get current location and re-initialize the map to it 
    //Search starting from your location 
    //----------------------------------------------------------------------------------- 


    var options = { 
     enableHighAccuracy: true, 
     timeout: 5000, 
     maximumAge: 0 
    }; 

    function success(pos) { 
     var crd = pos.coords; 

     var pyrmont = new google.maps.LatLng(crd.latitude, crd.longitude); 

     map = new google.maps.Map(document.getElementById('map-canvas'), { 
      center: pyrmont, 
      zoom: 14 
     }); 

     var request = { 
      location: pyrmont, 
      radius: 3000, 
      types: ['pharmacy'] 
     }; 
     infowindow = new google.maps.InfoWindow(); 
     var service = new google.maps.places.PlacesService(map); 
     service.nearbySearch(request, callback); 

     var myloc = new google.maps.Marker({ 
      clickable: false, 
      icon: new google.maps.MarkerImage('//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png', 
                 new google.maps.Size(22, 22), 
                 new google.maps.Point(0, 18), 
                 new google.maps.Point(11, 11)), 
      shadow: null, 
      zIndex: 999, 
      map: map 
     }); 

     var me = new google.maps.LatLng(crd.latitude, crd.longitude); 
     myloc.setPosition(me); 
    }; 

    function error(err) { 
     console.warn('ERROR(' + err.code + '): ' + err.message); 
    }; 
    //---------------------------------------------------------------------------------------------- 

    //Call get my location 
    navigator.geolocation.getCurrentPosition(success, error, options); 

    //Initialize the map 
    google.maps.event.addDomListener(window, 'load', initialize); 

</script>