使用地理编码初始化gmap3

时间:2012-05-25 10:14:10

标签: google-maps initialization jquery-gmap3

我是gmap3的新手,是否可以使用地址启动gmap3? 我知道它通常用纬度和经度来完成。 我使用this示例使用此位置名称完成了标记。 我已经通过了this例子,但没有运气。 而且我还需要缩放选择的位置,我使用自动完成为此目的。

我的代码如下:

 $('#test').gmap3(
     { action:'init',
         options:{
             address: "kerala,india"
         }
     }

 );

4 个答案:

答案 0 :(得分:4)

在成功设置中心时,首先使用getLatLng()检索位置:

   $('#test1').gmap3(
    { action : 'getLatLng',
      address:'kerala,india',
      callback : function(result){
        if (result){
          $(this).gmap3({action: 'setCenter', args:[ result[0].geometry.location ]},
                        {action: 'setZoom', args:[ 12]});
      } else {
      alert('not found !');
      }}});

答案 1 :(得分:2)

var add="kerala,india";
var geocoder = new google.maps.Geocoder();

geocoder.geocode({ address: add, region: 'no' },
function (results, status) {
 if (status.toLowerCase() == 'ok') {
    // Get center
    var coords = new google.maps.LatLng(
        results[0]['geometry']['location'].lat(),
        results[0]['geometry']['location'].lng()
    );
    //alert(coords);
    $map.gmap3("get").setCenter(coords);
    $map.gmap3("get").setZoom(12);
    addMarker(coords);
 }
 else {
    alert("address not found");
 }
}    

function addMarker(marker) {
    $map = $('#googleMap');
    if (marker) {
        $map.gmap3(
            { action: 'init',
                options: {
                    center: eval(marker),
                    zoom: 12,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
            },
            { action: 'addMarker',
                latLng: eval(marker),
                options: {
                    draggable: true,
                    icon: new google.maps.MarkerImage('http://maps.google.com/mapfiles/marker.png')
                },
                events: {
                    dragend: function (mark) {
                        updateMarker(mark);
                    }
                }
            });
    }
}

使用上面的代码,您也可以获得纬度/经度并拖动搜索的位置标记。

答案 2 :(得分:1)

此功能将在下一版本(5.0)中,当前未更新,

这是一个有效的代码:

  $('#test1').gmap3({ 
    action : 'getLatLng',
    address:'kerala,india',
      callback : function(result){
        if (result){
          $(this).gmap3({
            action: "init",
            options:{
                center: result[0].geometry.location,
                zoom: 12
            } 
        });
      } else {
        alert('not found !');
      }
    }
});

请注意,如果你的地图已经初始化,你可以在回调中使用

    $(this).gmap3("get").setCenter(result[0].geometry.location);
    $(this).gmap3("get").setZoom(12);

答案 3 :(得分:0)

您也可以使用以下代码:

var markerData = [];
markerData.push({ address: 'kerala,india', data: '' });
$('#test').gmap3({
   action: 'addMarkers',
   markers: markerData
   }
);
相关问题