标记点击事件无法在移动设备中使用

时间:2015-08-28 07:30:57

标签: angularjs google-maps google-maps-api-3 ionic infowindow

我为附近的美食广场创建了谷歌地图。在此标记显示在浏览器中并可点击并提供信息窗口数据。但同样的事情是移动,标记显示,当点击标记(点击标记)信息窗口数据不显示。我尝试了很多论坛并更改了大量代码和调试,但我无法找到解决方案。

foodFactory.js

var foodModule = angular.module('foodModule', []);
foodModule.factory("foodFactory", ['$rootScope', '$window','foodServices', 'localStorageService', '$state', '$ionicLoading','$stateParams', 
    function($rootScope,  $window, foodServices, localStorageService, $state, $ionicLoading, $stateParams, $cordovaGeolocation ) {

        var foodCourtmap = {};
        var marker = {};


        var directionsDisplay = new google.maps.DirectionsRenderer({'draggable': true });
        var directionsService = new google.maps.DirectionsService();

        foodCourtmap.centerOnMe = function() {
            initialize();               
      };


      //intialze the google map it's show current location.
    function initialize() {

          var infowindow = new google.maps.InfoWindow(); 

                navigator.geolocation.getCurrentPosition(function(pos) {

                foodCourtmap.latitude = pos.coords.latitude;
                foodCourtmap.longitude = pos.coords.longitude;

                var site = new google.maps.LatLng( foodCourtmap.latitude, foodCourtmap.longitude);

                var currentmapOptions = {
                    center: site,
                    zoom: 10,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                //current location address based on Latitude and Longitude 
                var lat = parseFloat(foodCourtmap.latitude);
                var lng = parseFloat(foodCourtmap.longitude);
                var latlng = new google.maps.LatLng(lat, lng);
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode({
                    'latLng': latlng
                }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[1]) {

                            var contentString = "Location: " + results[1].formatted_address;


                            var marker = new google.maps.Marker({
                                position: latlng,
                                map: map,
                                title: 'Current Location'

                            });
                            google.maps.event.addListener(marker, 'click', function(event) {

                                infowindow.setContent(contentString);
                                infowindow.open(map, marker);
                            });
                        }
                    }
                });


                var map = new google.maps.Map(document.getElementById("food_map_canvas"), currentmapOptions);

                // Places
                var request = {

                    location:site,
                    radius: '5000',
                    name: ['restaurent']

                };

            var service = new google.maps.places.PlacesService(map);
            service.search( request, callback );

            function callback(results, status) 
            {

                if (status == google.maps.places.PlacesServiceStatus.OK) {

                    for (var i = 0; i < results.length; i++) {

                        var place = results[i];
                        createMarker(results[i]);
                    }

                }

                else
                {
                       alert('No results found');
                }

            }

        var image = new google.maps.MarkerImage('img/Restaurant.png');


        function createMarker(place) {

                var placeLoc = place.geometry.location;

                var marker = new google.maps.Marker({
                  map: map,
                  title: place.name+","+place.vicinity,
                  position: place.geometry.location,
                  icon:image

                });

                var contentString =  place.name+","+place.vicinity;

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

                 foodCourtmap.map = map;

           });

       };

         $rootScope.createFoodCourt = function() {                        
                      foodCourtmap.centerOnMe();
                  } 

        return {

            init: function() {
                foodCourtmap.centerOnMe();
                return foodCourtmap;
            }
        };
    }
]);

food.html

<ion-view>
<ion-content scroll="false">

 <div id="food_map_canvas" data-tap-disabled="true" style="float:right;width:100%; height:100%"></div> 

</ion-content>
</ion-view>

所以请任何人帮助解决这些问题。

2 个答案:

答案 0 :(得分:5)

mousedown事件是一项改进,然而,在iOS上,事件仍然间歇性地为我发起。经过更多调查后,除了使用optimized: false事件之外,我还在创建标记时设置mousedown,找到了100%有效的解决方案。

E.g。

var newMarker = new google.maps.Marker({
                    position: latLong,
                    map: map,
                    icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png',
                    optimized: false
                });

https://code.google.com/p/gmaps-api-issues/issues/detail?id=3834

答案 1 :(得分:1)

我有同样的问题。问题是“点击”#39;当我们触摸移动屏幕时,事件不会触发。所以我改成了#mousedown&#39;事件。现在我可以添加标记