地理编码器api雷达搜索和多标记问题

时间:2017-05-11 05:02:53

标签: php jquery geocode

我的脚本如下。我必须搜索密码并根据搜索我必须在地图上显示位置

问题1:如果我有相同密码的2个结果,那么地图上还会有一个标记。我想显示单独的标记。

问题2:我想使用密码进行雷达搜索。如果密码07201和07202非常靠近区域,那么我应该得到两者的结果。我尝试在我的回复中使用radius,但它不起作用

<script>
      var map;
      <!-- var marker; -->
    function initialize() 
    { 
        var mapOptions = {
        center: new google.maps.LatLng('40.71338', '-74.193246'),
        zoom: 8,
        scrollwheel: false,
        disableDefaultUI: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
     };

      map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);

      marker = new google.maps.Marker({
                    position: new google.maps.LatLng(40.71338, -74.193246),
                    map: map,
                });


     }



    jQuery('#searchPincodeForm').submit(function (event) {
       event.preventDefault();

       var geocoder = new google.maps.Geocoder();
        var search_val=$('#country').val();

        jQuery.ajax({
                type: 'post',
                data:{
                    'pincode':search_val,                
                },
                url: 'searchPincode.php',           
                success:function(response){
                if(response != ''){

                $("#result").html(response).show();
                    geocoder.geocode({ 
                    'address': search_val }, function (results, status) {


                    if (status == google.maps.GeocoderStatus.OK) {

                    var position = results[0].geometry.location;
                        map.setCenter(position);
                        marker.setPosition(position);

                }

                })
                } else{

                    $("#result").html("Sorry Dealer not available at this location. For more information please mail at info@movemaxoil.com").show();

                }
                }


            });
            });


     </script>

我从地理编码得到的回复:

{
    "results": [{
        "address_components": [{
                "long_name": "07108",
                "short_name": "07108",
                "types": ["postal_code"]
            },
            {
                "long_name": "Newark",
                "short_name": "Newark",
                "types": ["locality", "political"]
            },
            {
                "long_name": "Essex County",
                "short_name": "Essex County",
                "types": ["administrative_area_level_2", "political"]
            },
            {
                "long_name": "New Jersey",
                "short_name": "NJ",
                "types": ["administrative_area_level_1", "political"]
            },
            {
                "long_name": "United States",
                "short_name": "US",
                "types": ["country", "political"]
            }
        ],
        "formatted_address": "Newark, NJ 07108, USA",
        "geometry": {
            "bounds": {
                "northeast": {
                    "lat": 40.73179289999999,
                    "lng": -74.1830549
                },
                "southwest": {
                    "lat": 40.713114,
                    "lng": -74.21835
                }
            },
            "location": {
                "lat": 40.7235275,
                "lng": -74.197388
            },
            "location_type": "APPROXIMATE",
            "viewport": {
                "northeast": {
                    "lat": 40.73179289999999,
                    "lng": -74.1830549
                },
                "southwest": {
                    "lat": 40.713114,
                    "lng": -74.21835
                }
            }
        },
        "place_id": "ChIJK0xWNjlTwokRLb_3zFLt8zM",
        "types": ["postal_code"]
    }],
    "status": "OK"
}

1 个答案:

答案 0 :(得分:0)

问题出在这里:

var position = results[0].geometry.location;
map.setCenter(position);
marker.setPosition(position);

您的ajax响应中可能包含多个位置点,但在上面的代码中,您只使用了第一个位置点results[0].geometry.location。因此,不是results[0]而是必须遍历所有结果数组,以便在您的标记上映射多个点。