Creating a business location map using Yahoo Local

Providing a store, client, or other location-listing feature is a common requirement for many Enterprise apps. In this recipe, we will show how to use the Yahoo Local Search API and Ti.Map.View to provide a store locator. For demonstration purposes, the recipe uses the Yahoo API to provide location search results for the popular American coffee chain, Starbucks. The search results for each location are then displayed on a Ti.Map.View, as shown in the following screenshots:

Creating a business location map using Yahoo Local

Getting ready

This recipe uses the Yahoo Search CommonJS module. This module and other code assets can be downloaded from the source code provided by the book. Installing this module into your project is straightforward. Simply add the yahoo_search.js file into your project, as shown in the following screenshot:

Getting ready

How to do it…

Once you've added the yahoo_search.js file to your project, you need to create your application namespaces in the app.js file and use require to import the module into your code, as shown in the following code block:

//Create our application namespace
var my = {
  search : require('yahoo_search'),
  isAndroid : ( Ti.Platform.osname === 'android')
};

Adding your API key

The next step in our recipe is to add your Yahoo developer API key. The following snippet shows how to register your API key with the module:

my.search.addAPIKey('YOUR_KEY_GOES_HERE'),

Creating a UI for the sample app

This recipe uses a simple UI containing Ti.UI.TextField, Ti.Map.View, and a Ti.UI.Button to search. Here we demonstrate the creation of these UI elements.

  1. First, we create a Ti.UI.Window to attach our visual elements.
      var win = Ti.UI.createWindow({
        backgroundColor:'#fff'
      });
  2. Next, we attach a text field to allow the user to enter a business for which to search.
      var txtSubject = Ti.UI.createTextField({
        left:0, width:150, height:40,top:30,
        hintText:'Enter Business Name',
        borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED
      });
      win.add(txtSubject);
  3. Next, a button is added to allow the user to search for the business name entered in the text field.
      var searchButton = Ti.UI.createButton({
        title:'Search', right:0, width:150, height:40,top:30	
      });
      win.add(searchButton);
  4. Finally, a map view is attached to display our search results.
      var mapView = Ti.Map.createView({
        mapType: Ti.Map.STANDARD_TYPE, width:Ti.UI.FILL,
        userLocation:true, top:75,bottom:0
      });
      win.add(mapView);

Updating the map

The updateMap function is the callback method to the search module. updateMap provides the search results and then it transforms them to display to the user.

  function updateMap(e){
  1. The search results are provided as the e parameter to the method. The first step in the process is to check if the search was successful, by checking the e.success property.
        if(!e.success){
          alert("Sorry we encountered an error");
          return;
        }
  2. After verifying that the search was successful, a loop is used to create map annotations for each e.item provided in the results.
        var points = [], itemCount = e.items.length, rightBtn = Ti.UI.iPhone.SystemButton.DISCLOSURE;
        for (var iLoop=0;iLoop<itemCount;iLoop++){
          points.push(Ti.Map.createAnnotation({
            latitude : e.items[iLoop].Latitude,
              longitude : e.items[iLoop].Longitude,
              title : e.items[iLoop].Address,
              subtitle : e.items[iLoop].Phone,
              pincolor : Ti.Map.ANNOTATION_GREEN,
              ClickUrl : items[iLoop].ClickUrl,
              animate:true, rightButton: rightBtn
          }));
        }
      
        var currentRegion = { latitude:e.latitude, longitude:e.longitude, latitudeDelta:0.04, longitudeDelta:0.04};
  3. The new region object is created using the search coordinates and applied to set the viewing point of the map. This allows the user to see all of the pins added.
          mapView.setLocation(currentRegion);
          mapView.regionFit = true;
  4. Finally, the points array containing all of our annotations is added to the mapView.
        mapView.annotations = points;
      };

Searching

When the searchButton button is pressed, the following snippet is used to perform a location search using the device's coordinates.

  searchButton.addEventListener('click',function(e){
  1. First, any existing map pins are removed
        mapView.removeAllAnnotations();
  2. If the text field's keyboard is open, the blur method is called to close it.
        txtSubject.blur();
  3. To avoid an empty search, the text field is checked to make sure it contains a value.
        if((txtSubject.value+'').length ===0){
          alert('Enter a business to search'),
          return;
        }
  4. The module's currentLocationQuery method is then called, providing the business name entered in the text field and the updateMap function to be used as a callback.
        my.search.currentLocationQuery(txtSubject.value,updateMap);
      });

How it works…

The Yahoo Search CommonJS module (yahoo_search.js) provides the following public functions, detailed in the following sections.

Using addAPIKey to your Yahoo service key

The Yahoo Local Search API requires a developer key. Before using any of the query methods, you must first use addAPIKey to associate your developer key with the module.

my.search.addAPIKey('YOUR_KEY_GOES_HERE'),

Tip

You can obtain a Yahoo API key by visiting developer.yahoo.com and creating a project within their developer portal.

Using the geoQuery method

The geoQuery function performs a Yahoo Local search using the latitude, longitude, and topic provided. The next example demonstrates how to search for Starbucks locations near Times Square in New York City. When the search has been completed, the results are provided to the callback function.

my.search.geoQuery(40.75773, -73.985708,'starbucks',callback);

Using the currentLocationQuery method

The currentLocationQuery method uses your device's location services to determine your current latitude and longitude. It then provides the geoQuery function with the required search details. The next code line demonstrates how to search for Starbucks outlets near your current position. Once the search has been completed, the results are provided to the callback function.

my.search.currentLocationQuery('starbucks',callback);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155