没有调用getCurrentPosition

时间:2015-11-20 14:51:53

标签: javascript

我试图将用户当前位置置于地图中心。

但我很难搞清楚为什么initMap: function() { var latLng; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function(position) { latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude) }, function() { latLng = new google.maps.LatLng(37.4, -122.1) }, { timeout: 10000 } ); }; var mapOptions = { center: latLng, zoom: 12, mapTypeId: google.maps.MapTypeId.ROADMAP }; this.oMap = new google.maps.Map(this.getView().byId("map_canvas").getDomRef(), mapOptions); this.geocoder = new google.maps.Geocoder(); this.directionsService = new google.maps.DirectionsService(); this.directionsDisplay = new google.maps.DirectionsRenderer({ draggable: true, map: this.oMap, suppressMarkers: true }); this.createCurrentLocationMarker(); this.loadDataFromModel(); } 被跳过了。当我调试它时,只需跳过该行。

我做错了什么?

$sql  = "INSERT INTO :submissionTable (project_id) VALUES (:projectId)";
$sql2 = "INSERT INTO ".static::$_mSubmissionTableName." (project_id) 
         VALUES ('".$values[0]."')";

$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare($sql);        
$result = $query->execute(array(
  ':submissionTable' => static::$_mSubmissionTableName, 
  ':projectId' => $values[0]
));

1 个答案:

答案 0 :(得分:1)

正如评论中提到的F​​rédéricHamidi,navigator.geolocation可能在您正在测试的浏览器中不可用。

但还有一些事情需要做。在填充之前,您无法访问latlang。因此,即使您在具有navigator.geolocation的浏览器中进行测试,也必须将代码更改为如下所示。

initMap: function() {
    var latLng;

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                var mapOptions = {
                      center: latLng,
                      zoom: 12,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                this.oMap = new google.maps.Map(this.getView().byId("map_canvas").getDomRef(), mapOptions);
                this.geocoder = new google.maps.Geocoder();
                this.directionsService = new google.maps.DirectionsService();
                this.directionsDisplay = new google.maps.DirectionsRenderer({
                     draggable: true,
                     map: this.oMap,
                     suppressMarkers: true
                 });

                 this.createCurrentLocationMarker();
                 this.loadDataFromModel();
            }.bind(this), function() {
                latLng = new google.maps.LatLng(37.4, -122.1)
            }, {
                timeout: 10000
            }
        );
    }; 
}

现在,您的代码将在latlang可用后运行,并且还会注意.bind(this)以确保回调中this相同。