Instagram Searching for media within a given radius

Searching for media within a given radius

In the media endpoint, the Search method lets you search for media within a given radius of a specified GPS coordinate. The Search has a number of overloads, but in the simplest form you can call the method like:

@using Skybrud.Social.Instagram.Objects.Media @using Skybrud.Social.Instagram.Responses.Media @inherits WebViewPage

@{

// Search for media in a 1 km radius of our HQ in Vejle
InstagramGetRecentMediaResponse response = Model.Media.Search(55.708112, 9.536118);

foreach (InstagramMedia media in response.Body.Data) {

    <div>
        <a href="@media.Link"><img src="@media.LowRes" /></a>
        <div>
            @media.Location.Name
        </div>
        <div>
            @media.CaptionText
        </div>
    </div>

}

}

By default, the API will search for media within a radius of 1000 metres (1 km), but you can also specify the radius explicitly like:

@using Skybrud.Social.Instagram.Objects.Media @using Skybrud.Social.Instagram.Responses.Media @inherits WebViewPage

@{

// Search for media in a 5 km radius of our HQ in Vejle
InstagramGetRecentMediaResponse response = Model.Media.Search(55.708112, 9.536118, 5000);

foreach (InstagramMedia media in response.Body.Data) {

    <div>
        <a href="@media.Link"><img src="@media.LowRes" /></a>
        <div>
            @media.Location.Name
        </div>
        <div>
            @media.CaptionText
        </div>
    </div>

}

}

The maximum allowed radius is 5000 metres (5 km).

Another overload of the Search method takes an instance of the ILocation interface from our Skybrud.Essentials package (the EssentialsLocation class implements the interface):

@using Skybrud.Essentials.Locations @using Skybrud.Essentials.Locations.Extensions @using Skybrud.Social.Instagram.Objects.Media @using Skybrud.Social.Instagram.Responses.Media @inherits WebViewPage

@{

// Skybrud.dk HQ
EssentialsLocation location = new EssentialsLocation(55.708112, 9.536118);

// Search for media in a 5 km radius of our HQ in Vejle
InstagramGetRecentMediaResponse response = Model.Media.Search(location, 5000);

foreach (InstagramMedia media in response.Body.Data) {

    <div>
        <a href="@media.Link"><img src="@media.LowRes" /></a>
        <div>
            @media.Location.Name (@location.GetDistance(media.Location).ToString("0.00") metres)
        </div>
        <div>
            @media.CaptionText
        </div>
    </div>

}

}

While the Search method and the Instagram also supports a few other parameters, these are undocumented. You can specify these through an instance of

@using Skybrud.Essentials.Locations @using Skybrud.Essentials.Time @using Skybrud.Social.Instagram.Objects.Media @using Skybrud.Social.Instagram.Options.Media @using Skybrud.Social.Instagram.Responses.Media @inherits WebViewPage

@{

// Skybrud.dk HQ
EssentialsLocation location = new EssentialsLocation(55.708112, 9.536118);

// Initialize the options for the call
InstagramGetRecentMediaOptions options = new InstagramGetRecentMediaOptions();
options.Latitude = location.Latitude;
options.Longitude = location.Longitude;
options.Distance = 5000;

// Undocumented parameters
options.Count = 50;
options.MaxTimestamp = new EssentialsDateTime(2017, 4, 27, 20, 0, 0);
options.MinTimestamp = new EssentialsDateTime(2017, 4, 27, 18, 0, 0);


// Search for media in a 5 km radius of our HQ in Vejle
InstagramGetRecentMediaResponse response = Model.Media.Search(options);

// Iterate through the media in the response
foreach (InstagramMedia media in response.Body.Data) {

    <p>@media.CaptionText</p>

}

}

The MaxTimestamp and MinTimestamp parameters can for instance be used for pagination.