angularjs

I'm trying to implement Google maps in Angularjs using ui.Map (http://angular-ui.github.io/ui-map/)

I've followed the example pretty closely and the map loads, I can create a marker in the map center and the 'map-tilesloaded' event works fine.

My problem is adding a marker where the user clicks. The click function is receiving an empty $params parameter. In my controller:

$scope.newMapOptions = {
    center : new google.maps.LatLng($scope.position.lat, $scope.position.lng),
    zoom : 18,
    mapTypeId : google.maps.MapTypeId.ROADMAP
};
$scope.getLocation = function() {
    if (navigator.geolocation) {
        return navigator.geolocation.getCurrentPosition(setPosition);
    }
};

$scope.addMarker = function($event, $params) {
    $scope.newTingMarker = new google.maps.Marker({
        map : $scope.myNewTingMap,
        position : $params[0].latLng
    });
};

$scope.initMap = function() {
    if (!$scope.mapLoaded)
        $scope.getLocation();
    $scope.mapLoaded = true;
};

function setPosition(pos) {
    $scope.position = {
        lat : pos.coords.latitude,
        lng : pos.coords.longitude
    };
    $scope.meMarker = new google.maps.Marker({
        map : $scope.myNewTingMap,
        position : new google.maps.LatLng($scope.position.lat, $scope.position.lng)
    });

    $scope.myNewTingMap.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
    $scope.$apply();
}

The html:

<div ui-map-info-window="myInfoWindow">
    <b>Current location</b>
</div>
<div  ui-map-marker="meMarker" ></div>
<div  ui-map-marker="newTingMarker" ui-event="{'map-click': 'openMarkerInfo(newTingMarker)'}"></div>
<section id="newTingMap" >
    <div ui-map="myNewTingMap" ui-options="newMapOptions" class="map-canvas"
    ui-event="{'map-tilesloaded': 'initMap()', 'map-click': 'addMarker($event, $params)' }"></div>
</section>

$scope.addMarker should receive $event and $params where $params[0] has the latlng object. At the moment is it an empty array: []

I'm using angular 1.1.5, but I've tried using the same as the ui.Map example with no effect.

I should also note that this is in a view but putting it outside the view in the main controller makes no difference.

If I try to follow the code running from the ui-map directive I can see that the latlng object does start off in the event:

ui-map.js:

 angular.forEach(eventsStr.split(' '), function (eventName) {
  //Prefix all googlemap events with 'map-', so eg 'click' 
  //for the googlemap doesn't interfere with a normal 'click' event
  google.maps.event.addListener(googleObject, eventName, function (event) {
    element.triggerHandler('map-' + eventName, event);
    //We create an $apply if it isn't happening. we need better support for this
    //We don't want to use timeout because tons of these events fire at once,
    //and we only need one $apply
    if (!scope.$$phase){ scope.$apply();}
  });
});

element.triggerHandler('map-' + eventName, event); ... has the latlng object in 'event' but is seems to get lost after that


Not sure what your issue is, I took your code and created a fiddle that works fine(something you should have done).

I did a console log when you click that logs the $params.

The most important thing to note is your code crashes at first because you reference $scope.position.lat before setting it. I updated it to default to RVA.

在这里输入图像描述 ,

You do need to handle the case a little more gracefully.

function MapCtrl($scope, watchArray) {
    var center;
    if ($scope.position) {
        center = new google.maps.LatLng($scope.position.lat, $scope.position.lng);
    } 
    else {
        center = new google.maps.LatLng(37.5410, 77.4329); //if null use rva
    }
    $scope.newMapOptions = {
        center: center,
        zoom: 18,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    ...
}

Console.log:

[Ps]
v 0: Ps
    > la: Q
    > latLng: O
    > pixel: Q
    > __proto__: Ps
    length: 1
    > __proto__: Array[0]
链接地址: http://www.djcxy.com/p/17238.html

上一篇: 如何解决Intellij Idea的所有检查样式问题?

下一篇: angularjs