当鼠标光标纬度等于标记纬度时,发出警报(Google Maps API v3)

Alert a message when mouse cursor latitude is equal to markers latitude (Google Maps API v3)

本文关键字:纬度 Google Maps API v3 光标 鼠标 于标记      更新时间:2024-06-25

我最近开始研究谷歌地图API V3,现在我陷入了一个问题。

我在地图上画了两个记号。我想要的是,在地图上移动鼠标光标时,它会不断检查纬度,当纬度等于地图上标记的纬度时,它应该显示一条警告消息。

我面临的问题是,它只适用于一个标记,而不适用于地图上的所有标记

这是我的代码:

$.ajax({
        type: "GET",
        url: $('meta[name="_home_url"]').attr('content') + '/walkalong/' + position.coords.latitude + "/" + position.coords.longitude,
        success: function (response) {
            // Looping through all the entries from the JSON data
            for (var i = 0; i < response.data.shops.length; i++) {
                // Current object
                var obj = response.data.shops[i];
               // Sets the map marker
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(obj.latitude, obj.longitude),
                    map: map,
                    info: contentString,
                    optimized: false,
                    icon: pinIcon,
                    id: obj.shop_id,
                    animation: google.maps.Animation.DROP,
                    title: obj.title // this works, giving the marker a title with the correct title
                });
                google.maps.event.addListener(map, 'mousemove', function (event) {
                    Latfetch = event.latLng.lat().toFixed(4);
                    Longfetch = event.latLng.lng().toFixed(4);
                    if (Latfetch === obj.latitude) {
                        alert(obj.latitude);
                    }
                });

JSON响应为:

{"status":200,"data":{"shops":[{"shop_id":7,"name":"reebok","logo":"543813f8-cc75-4217-a3d4-bb08992f66af.png","latitude":"17.4539","longitude":"78.3902","shop_description":"","opening_time":"09:00:00","closing_time":"18:00:00","mobile":"4567896545","address_line1":"ameerpet","city":"Dispur","state":"Arunachal Pradesh","country":"India","pincode":"435645","baazar_area":"","front_image":"6c08a359-fb7f-4e09-af25-3c2d43a32327.jpg","distance":0.703},{"shop_id":9,"name":"ahex","logo":"86c6f7f8-625c-4a7a-aee4-10f7d16f1f12.jpg","latitude":"17.4519","longitude":"78.3879","shop_description":"","opening_time":"09:00:00","closing_time":"18:00:00","mobile":"4233488384","address_line1":"madhapur","city":"Panaji","state":"Goa","country":"India","pincode":"345678","baazar_area":"","front_image":"31e96273-c360-4b49-87d1-418bdac26dc3.jpg","distance":0.711}],"shopImages":[{"logo":"543813f8-cc75-4217-a3d4-bb08992f66af.png","front_image":"6c08a359-fb7f-4e09-af25-3c2d43a32327.jpg","internal_image":"e988011f-f2c1-4b08-8582-0f36e85dc4f9.jpg","rack_image":"rack.jpeg","product_image":"item.jpeg"}]}}

有谁能帮忙吗?

我只能猜测它只适用于一个,因为您只能为mousemove添加一个侦听器。您可以在标记上添加事件:

 for (var i = 0; i < response.data.shops.length; i++) {
    ...
    var marker = new google.maps.Marker({
        ...
    });
    marker.addListener('mousemove', function(event) {
         var latfetch = event.latLng.lat().toFixed(4);
         var longfetch = event.latLng.lng().toFixed(4);
         var lat = this.position.lat();
         if(latfetch === lat){
              alert(lat);
         }
    });
}