आप JSONP परिणाम है कि google.maps.places.Autocomplete कार्यक्षमता द्वारा लौटाए गए रोकना और उन्हें इस्तेमाल आप मनचाहे ढंग से, जैसे देश से सीमित करने और परिणामों को प्रदर्शित कर सकते हैं।
मूल रूप से आप मुख्य तत्व पर appendChild विधि को फिर से परिभाषित करते हैं, और उसके बाद जावास्क्रिप्ट तत्वों की निगरानी करते हैं जो Google स्वत: पूर्ण कोड JSONP के लिए DOM में डालें। चूंकि जावास्क्रिप्ट तत्व जोड़े जाते हैं, इसलिए आप जेएसओएनपी कॉलबैक को ओवरराइड करते हैं जिसे Google कच्चे स्वत: पूर्ण डेटा तक पहुंच प्राप्त करने के लिए परिभाषित करता है।
यह एक हैक का एक सा, यहाँ जाता है (मैं jQuery का उपयोग कर रहा हूँ, लेकिन यह आवश्यक इस हैक काम करने के लिए नहीं है) है:
//The head element, where the Google Autocomplete code will insert a tag
//for a javascript file.
var head = $('head')[0];
//The name of the method the Autocomplete code uses to insert the tag.
var method = 'appendChild';
//The method we will be overriding.
var originalMethod = head[method];
head[method] = function() {
if (arguments[0] && arguments[0].src && arguments[0].src.match(/GetPredictions/)) { //Check that the element is a javascript tag being inserted by Google.
var callbackMatchObject = (/callback=([^&]+)&|$/).exec(arguments[0].src); //Regex to extract the name of the callback method that the JSONP will call.
var searchTermMatchObject = (/\?1s([^&]+)&/).exec(arguments[0].src); //Regex to extract the search term that was entered by the user.
var searchTerm = unescape(searchTermMatchObject[1]);
if (callbackMatchObject && searchTermMatchObject) {
var names = callbackMatchObject[1].split('.'); //The JSONP callback method is in the form "abc.def" and each time has a different random name.
var originalCallback = names[0] && names[1] && window[names[0]] && window[names[0]][names[1]]; //Store the original callback method.
if (originalCallback) {
var newCallback = function() { //Define your own JSONP callback
if (arguments[0] && arguments[0][3]) {
var data = arguments[0][4]; //Your autocomplete results
//SUCCESS! - Limit results here and do something with them, such as displaying them in an autocomplete dropdown.
}
}
//Add copy all the attributes of the old callback function to the new callback function. This prevents the autocomplete functionality from throwing an error.
for (name in originalCallback) {
newCallback[name] = originalCallback[name];
}
window[names[0]][names[1]] = newCallback; //Override the JSONP callback
}
}
//Insert the element into the dom, regardless of whether it was being inserted by Google.
return originalMethod.apply(this, arguments);
};
यह सही उत्तर –
घटक रेस्त्रां है: {देश: 'gb'} // केवल – geedubb
प्रश्न के अनुसार यूके तक ही सीमित होगा, यह 'प्रकार: ['शहरों']' 'नहीं होना चाहिए: ['(शहर) '] ' – tirdadc