1

I want to show Google map in my html . here is my code:

html:

    <div class="map-content">
      <div map-marker="" ng-model="searchLocation" class="mapmarker"></div>
    </div>

js:

app.directive('mapMarker',function(){
    return {
        restrict: 'EA',
        require: '?ngModel',
        scope:{
            searchLocation: '=ngModel'
        },
        controller: function ($scope) {
            $scope.searchLocation = {
                latitude: 48.137273,
                longitude: 11.575251
            };
        },
        resolve: {
            load: function () {

            }
        },
        link: function(scope , element, attrs , ngModel){

            var mapOptions;
            var googleMap;
            var searchMarker;
            var searchLatLng;

            ngModel.$render = function(){

                searchLatLng = new google.maps.LatLng(scope.searchLocation.latitude, scope.searchLocation.longitude);

                mapOptions = {
                    center: searchLatLng,
                    zoom: 12,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                googleMap = new google.maps.Map(element[0],mapOptions);

                searchMarker = new google.maps.Marker({
                    position: searchLatLng,
                    map: googleMap,
                    draggable: true
                });

                google.maps.event.addListener(searchMarker, 'dragend', function(){

                    scope.$apply(function(){
                        scope.searchLocation.latitude = searchMarker.getPosition().lat();
                        scope.searchLocation.longitude = searchMarker.getPosition().lng();
                    });
                }.bind(this));

            };

            scope.$watch('searchMarker', function(value){
                var myPosition = new google.maps.LatLng(scope.searchLocation.latitude, scope.searchLocation.longitude);
                searchMarker.setPosition(myPosition);
            }, true);
        }
    }
});

Also, I include

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

It knows script file but it can't show map on my html. Any suggestion?

2
  • What does console say? Commented Jun 19, 2016 at 13:06
  • where do you included the api script? Commented Jun 19, 2016 at 13:25

1 Answer 1

1

With a little bit of modifications your example works: see my plunkr.

angular
  .module('app', [])
  .directive('mapMarker',function(){
    return {
        restrict: 'EA',
        scope:{
            searchLocation: '=mapMarker'
        },
        controller: function ($scope) {
            $scope.searchLocation = {
                latitude: 48.137273,
                longitude: 11.575251
            };
        },
        link: function(scope , element, attrs , ngModel){
            var mapOptions;
            var googleMap;
            var searchMarker;
            var searchLatLng;

            searchLatLng = new google.maps.LatLng(scope.searchLocation.latitude, scope.searchLocation.longitude);

            mapOptions = {
                center: searchLatLng,
                zoom: 12,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            googleMap = new google.maps.Map(element[0], mapOptions);

            searchMarker = new google.maps.Marker({
                position: searchLatLng,
                map: googleMap,
                draggable: true
            });

            google.maps.event.addListener(searchMarker, 'dragend', function(){
                scope.$apply(function(){
                    scope.searchLocation.latitude = searchMarker.getPosition().lat();
                    scope.searchLocation.longitude = searchMarker.getPosition().lng();
                });
            });

            scope.$watch('searchMarker', function(value){
                var myPosition = new google.maps.LatLng(scope.searchLocation.latitude, scope.searchLocation.longitude);
                searchMarker.setPosition(myPosition);
            }, true);
        }
    }
  });
  1. You don't need ngModel. Especially ngModel.$render. It is needed for inputs, which is not your case.
  2. Container for map has to have initial size: width and height.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.