سؤال

أحاول استخدام Google Geocoder API V3 لرسم موقع على خريطة بناءً على عنوان محدد من قبل المستخدم ، فإن الكود أدناه.

عندما أقوم بطلب مباشرة (على سبيل المثال إلى http://maps.googleapis.com/maps/api/geocode/json؟address=peterborough&sensor=false) أحصل على الرد المتوقع. ومع ذلك ، عندما أقوم بتقديم نفس الطلب باستخدام الرمز أدناه ، نقطة الوسط المتغير دائمًا غير محدد بعد getlatlong لقد خرجت الوظيفة.

ماذا أفعل بشكل غير صحيح؟

function loadFromSearch(address) 
{
  midpoint = getLatLong(address);
  mapCentre = midpoint;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}


function getLatLong(address) 
{
  var result;
  var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
  $.getJSON(url,
  function (data){
     if (data.status == "OK")
     {
        result = data.results[0].geometry.location;
     }
  });
  return result;
}

==================================================================================

في ضوء الردود ، قمت الآن بتحديث الرمز إلى ما يلي. ما زلت لا أحصل على أي نتيجة ، مع تعيين نقاط التوقف في Firebug النتيجة = data.Results [0] .Geometry.location ؛ لا تصيب أبدا.

function loadFromSearch(address) 
{
  midpoint = getLatLong(address, loadWithMidpoint);    
}

function getLatLong(address, callback)
{
   var result;
   var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
   $.getJSON(url,{},
   function (data) {
     if (data.status == "OK")
     {
        result = data.results[0].geometry.location;
        callback(result);
     }
   });
}

function loadWithMidpoint(centre)
{
  mapCentre = centre;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}

=============================================================================

انا أملكه! الرمز النهائي ، الذي يعمل ، يشبه هذا:

function loadFromSearch(coordinates, address)
{
  midpoint = getLatLong(address, latLongCallback);
}

function getLatLong(address, callback)
{
  var geocoder = new google.maps.Geocoder();
  var result = "";
  geocoder.geocode({ 'address': address, 'region': 'uk' }, function (results, status) {
     if (status == google.maps.GeocoderStatus.OK)
     {
        result = results[0].geometry.location;
        latLongCallback(result);
     }
     else
     {
        result = "Unable to find address: " + status;
     }
  });
  return result;
}

function latLongCallback(result)
{
  mapCentre = result;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}
هل كانت مفيدة؟

المحلول

إذا كنت تستخدم V3 من API ، فهل يمكنك استخدام هذا؟

function findAddressViaGoogle() {
    var address = $("input[name='property_address']").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            newPointClicked(results[0].geometry.location)
        } else {
            alert("Unable to find address: " + status);
        }
    });
}

ما سبق هو ما أستخدمه للعثور على بعض اللورمتين الطويلتين لعنوان المدخلات ، هل قد يعمل بشكل أفضل؟

تعديل:

function loadFromSearch(address) 
{
midpoint = getLatLong(address);
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}


function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    return result;
}

نصائح أخرى

المشكلة هي $.getJSON الوظيفة غير متزامنة ، ومع ذلك فأنت تعود إلى 'result'بشكل متزامن.

تحتاج إلى فعل شيء مثل هذا (لم يتم اختباره!)

function loadFromSearch(address) 
{
  midpoint = getLatLong(address, function(midpoint){
    // this is a callback
    mapCentre = midpoint;
    map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
    ...           
    });
}

function getLatLong(address, callback) 
{
var result;
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
$.getJSON(url,
  function (data) {
    if (data.status == "OK") {
        result = data.results[0].geometry.location;
        callback(result) // need a callback to get the asynchronous request to do something useful 
    }
  });
}

ردا على تحريرك: يا عزيزي ، يبدو أن V3 Geocoder لا يدعم JSONP. هذا يعني أنه لا يمكنك تقديم طلب عبر المجال للحصول على بيانات منه في متصفحك. نرى http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

ومع ذلك ، فإن حل برادي يعمل. أعتقد أن هذه هي الطريقة التي تريدنا Google أن نرسمها الآن.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top