2012-05-04 26 views
8

पर किसी अन्य पैरामीटर में पास करता है मैं मानचित्र पर एक अलग मार्कर द्वारा प्रतिनिधित्व किए जाने वाले प्रत्येक 'प्रकार' स्थानों को वापस करने के लिए Google मानचित्र स्थान API v3 का उपयोग कर रहा हूं।Google मानचित्र API v3 स्थान खोज - कॉलबैक फ़ंक्शन

मैं एक google.maps.places.PlacesService ऑब्जेक्ट बना देता हूं, और फिर प्रति स्थान प्रकार में एक बार "खोज" विधि को कॉल करता हूं। प्रत्येक बार, मैं एक अलग कॉलबैक फ़ंक्शन ("खोज" का दूसरा पैरामीटर) का उपयोग करता हूं, क्योंकि मुझे प्रत्येक प्रकार के लिए एक अलग मार्कर इमेज चुनना होगा। (

function banks(results, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
     for (var i = 0; i < results.length; i++) { 
      createMarker(results[i], new google.maps.MarkerImage("/images/bank.png", null, null)); 
     } 
    } 
} 
function bars(results, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
     for (var i = 0; i < results.length; i++) { 
      createMarker(results[i], new google.maps.MarkerImage("/images/bar.png", null, null)); 
     } 
    } 
} 
function carparks(results, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
     for (var i = 0; i < results.length; i++) { 
      createMarker(results[i], new google.maps.MarkerImage("/images/parking.png", null, null)); 
     } 
    } 
} 

इस कोड को 100% काम करता है, लेकिन मैं प्रत्येक अलग जगह प्रकार के लिए कॉलबैक डुप्लिकेट करने से बचने के लिए चाहते हैं वहाँ होगा:

var address = "97-99 Bathurst Street, Sydney, 2000"; 
geocoder.geocode({ 'address': address }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var location = results[0].geometry.location; 

     map.setCenter(location); 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: location 
     }); 

     infowindow = new google.maps.InfoWindow(); 
     var service = new google.maps.places.PlacesService(map); 

     // banks 
     var req_bank = { location: location, radius: 500, types: ['bank'] }; 
     service.search(req_bank, banks); 

     // bars 
     var req_bar = { location: location, radius: 500, types: ['bar'] }; 
     service.search(req_bar, bars); 

     // car parks 
     var req_parking = { location: location, radius: 500, types: ['parking'] }; 
     service.search(req_parking, carparks); 

    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 

यहाँ कॉलबैक काम करता है, जो केवल MarkerImage से भिन्न होते हैं लगभग 10 हो)। क्या कोई तरीका है कि मैं कॉलर फ़ंक्शन में मार्कर यूआरएल पास कर सकता हूं? तब मैं केवल ... एक भी कॉलबैक की आवश्यकता होगी

उत्तर

7

कैसे के बारे में निम्नलिखित:

service.search(req_bank, function (results, status) { 
    locations(results, status, "bank"); 
}); 

function locations(results, status, type) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
    // check the type to determine the marker, or pass a url to the marker icon 
    } 
} 
+0

बहुत बढ़िया ... बस क्या मैं :) – howlee

+0

Doy के बाद हो गया था! मुझे पता होना चाहिए था। धन्यवाद! –

संबंधित मुद्दे