简体   繁体   English

Maps API v3地理编码

[英]Maps API v3 geocoding

Straight to the point: I'm building a gmap with custom markers pulled via JSON. 直截了当:我正在构建一个带有通过JSON提取自定义标记的gmap。 I also need to provide a search functionality; 我还需要提供搜索功能; the nearest marker to the inputted postcode (AU). 最接近输入邮政编码(AU​​)的标记。

Marker display (and other data from the array) works fine, it's this second part that has my ass kicked. 标记显示(以及数组中的其他数据)工作正常,正是这第二部分使我的屁股踢了。 I'm not great with JS. 我对JS不太满意。 At the moment all I'm trying to do is return the Lat and Long of the inputted postcode, though if anyone can show me how to perform the whole function, that would be great. 目前,我想做的就是返回输入邮政编码的纬度和经度,尽管如果有人可以向我展示如何执行整个功能,那将很棒。 Can anyone point me in the right direction? 谁能指出我正确的方向? JS and markup below, and thanks in advance for any assistance. 下面是JS和标记,在此先感谢您的协助。

JavaScript: JavaScript的:

function initialize() {
  /* Map setup */
  var latlng = new google.maps.LatLng(XXX, XXX);
  var mapOptions = {
     center: latlng,
     zoom: 16,
     mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  /* Map initialise */
  var map = new google.maps.Map(document.getElementById("map-canvas"),
     mapOptions);

  /* set iconpath */
  var iconBase = '/path-to-icons';

  /* create a separate shadow icon */
  var markerShadow = {
     url: iconBase + 'icon_map-shadow.png',
     anchor: new google.maps.Point(15, 25)
  };

  /* Get JSON */
  jQuery.getJSON('/path-to/markers.json', function (mapdata) {
      $.each(mapdata, function (key, data) {
        var latlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
           position: latlng,
           map: map,
           icon: iconBase + 'icon_map.png',
           shadow: markerShadow,
           address: data.address,
           subaddress: data.subaddress,
           title: data.title
        });
        /* For the showing of the details */
        // TODO : MAKE THIS NEATER
        google.maps.event.addListener(marker, 'click', function () {
           $("#map-details .inner").hide();
           $("#map-details .inner span").html("");
           $("#map-details .inner .neareststore").html(marker.title);
           $("#map-details .inner .address").html(marker.address);
           $("#map-details .inner .sub-address").html(marker.subaddress);
           $("#map-details .inner").fadeIn(200);
        });
     });
  });
};
google.maps.event.addDomListener(window, 'load', initialize);

function geocode() {
  $("#search").submit(function(e){
    e.preventDefault();
    var currentloc = $("#postcode").val();
    var url = "http://maps.googleapis.com/maps/api/geocode/json?address="+currentloc+"+AU&sensor=false";
    $.getJSON(url, function(data) {
      //JSON function
    }); 
  };
};

Basic form HTML: 基本表单HTML:

<form action="" method="post" id="search">
  <fieldset>
    <input name="postcode" id="postcode">
    <input type="submit" id="postcodesearch" value="search">
  </fieldset>
</form>




* EDIT * - Answered Can't answer my own question yet, but have solved this using the Haversine formula linked here: Google Maps Api v3 - find nearest markers *编辑* -已回答无法回答我自己的问题,但已使用链接到此处的Haversine公式解决了此问题: Google Maps Api v3-查找最近的标记

It needs some tidying but this is the gist, here for posterity. 它需要进行一些整理,但这是要点,这里供后人参考。 Thanks for the assistance all. 感谢大家的帮助。

function rad(x) {return x*Math.PI/180;}
function find_closest_marker( _lat , _lng  ) {

    var lat = _lat;
    var lng = _lng;
    var R = 6371; // radius of earth in km
    var distances = [];
    var closest = -1;
    for( i=0;i<map.markers.length; i++ ) {
        var mlat = map.markers[i].position.lat();
        var mlng = map.markers[i].position.lng();
        var dLat  = rad(mlat - lat);
        var dLong = rad(mlng - lng);
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;
        distances[i] = d;
        if ( closest == -1 || d < distances[closest] ) {
            closest = i;
        }
    }
    var closest = map.markers[closest];

    map.setCenter(closest.getPosition());
    $("#map-details .inner").hide();
    $("#map-details .inner span").html("");
    $("#map-details .inner .neareststore").html(closest.title);
    $("#map-details .inner .address").html(closest.address);
    $("#map-details .inner .sub-address").html(closest.subaddress);
    $("#map-details .inner").fadeIn(200);
}

$(document).ready(function(){
    $("#search").submit(function(e){
      e.preventDefault();
      var currentloc  = $("#postcode").val();
      var url         = "http://maps.googleapis.com/maps/api/geocode/json?address="+currentloc+"+AU&sensor=false";
      $.getJSON(url, function(data) {

        var lat = data.results[0].geometry.location.lat;
        var lng = data.results[0].geometry.location.lng;
        find_closest_marker(lat,lng)
      });
    });
});

Your second-to-last line has a syntax error; 您倒数第二行的语法错误; it should be }); 应该是}); instead of }; 代替};

It should be this: 应该是这样的:

    function geocode() {
  $("#search").submit(function(e){
    e.preventDefault();
    var currentloc = $("#postcode").val();
    var url = "http://maps.googleapis.com/maps/api/geocode/jsonaddress="+currentloc+"+AU&sensor=false";
    $.getJSON(url, function(data) {
      //JSON function
    }); 
  });
};

I used 我用了 http://www.dirtymarkup.com/ to check for syntax errors. http://www.dirtymarkup.com/检查语法错误。 It's really useful. 这真的很有用。

And, from https://developers.google.com/maps/documentation/geocoding/#JSON , it tells you the JSON output format. 并且,从https://developers.google.com/maps/documentation/geocoding/#JSON ,它告诉您JSON输出格式。 So, to get the Lat/Long, you would use: 因此,要获得纬度/经度,您可以使用:

data.results.geometry.location.lat and data.results.geometry.location.lng data.results.geometry.location.latdata.results.geometry.location.lng

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM