Tag Archives: bing.com

GMap.NET component : Moving Marker

WTH is GMap.NET?

Simply put: it’s an open source .net component that can show online mapping service (such as Google Map, Bing Map, OpenStreetMap) in your .NET app.
Download the binary here: http://greatmaps.codeplex.com/
Extract it. You’ll get the .net 2.0 and .net 4.0 version, since I’m using VS 2015, we’ll use the .net 4.0 version.

What will we do now is about how to move a marker inside the map (for tracking/visualizing object movement, etc). We’ll just move the marker randomly in this tutorial.

  • Create new VB Windows Form project
  • Add GMap.NET component into your project reference
GMap.NET component reference

GMap.NET component reference

  • Add GMap.NET component into your toolbar.
Add GMap.NET component step 1

Add GMap.NET component step 1

Add GMap.NET component step 2

Add GMap.NET component step 2

  • Now drag and drop the GMap.NET control into your Form, I name it myMap
  • We’ll also add another controls: 1 Button (name: btnMove), 1 Timer (name: tmr1), 1 Label (name: lblInfo)
List of components

List of components

I also add new bitmap resource into our project properties. This will be used as our marker symbol.

Resource for marker symbol

Resource for marker symbol

You can see the Form’s full source code at the bottom of this post. I’ll just point out some important parts:

  • Map initialization is on Form1_Load event code (zoom level, initial position, provider used, marker type, etc)
  • When you click on the btnMove, it’ll start generating new random position for our marker. The random change is happened in the timer event (tmr1_Tick)
  • The process to update the map visual is delegated into function update_map(), so it will run in different thread and the form still responsive
  • In this tutorial, we are using BingSatelliteMapProvider, you can use another provider such as GoogleMapProvider or OviSatelliteMapProvider. Just see MapProviders namespace to see all supported providers.

Running the app

Running the app


Here’s the full source code:

Imports GMap.NET
Imports GMap.NET.WindowsForms

Public Class Form1
    Private Delegate Sub UpdateFormDelegate()
    Private UpdateFormDelegate1 As UpdateFormDelegate

    Private copter_lat, copter_lon As Double
    Private WithEvents copter_marker As Markers.GMarkerGoogle
    Private WithEvents copter_layer As GMapOverlay

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        copter_lat = -7.076275
        copter_lon = 110.428952

        myMap.MinZoom = 5
        myMap.MaxZoom = 20
        myMap.Zoom = 17
        myMap.Position = New PointLatLng(-7.076275, 110.428952)
        myMap.MapProvider = MapProviders.BingSatelliteMapProvider.Instance
        myMap.Manager.Mode = AccessMode.ServerAndCache
        copter_layer = New GMapOverlay("copter_layer")
        copter_marker = New Markers.GMarkerGoogle(New PointLatLng(-7.076275, 110.428952), My.Resources.purple_drone)
        copter_layer.Markers.Add(copter_marker)
        myMap.Overlays.Add(copter_layer)
        myMap.UpdateMarkerLocalPosition(copter_marker)
        myMap.Invalidate()
        update_map()
    End Sub

    Private Sub tmr1_Tick(sender As Object, e As EventArgs) Handles tmr1.Tick
        Randomize()
        Dim delta_lat As Double = ((100 * Rnd()) + 20) / 10000000.0
        Dim delta_lon As Double = ((100 * Rnd()) + 20) / 10000000.0
        Dim not_lat As Single = Rnd()
        Dim not_lon As Single = Rnd()

        If not_lat >= 0.5 Then
            copter_lat += delta_lat
        Else
            copter_lat -= delta_lat
        End If
        If not_lon >= 0.5 Then
            copter_lon += delta_lon
        Else
            copter_lon -= delta_lon
        End If

        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf update_map)
        Invoke(UpdateFormDelegate1)
    End Sub

    Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click
        If tmr1.Enabled Then
            tmr1.Enabled = False
            MsgBox("Random Move stop")
        Else
            tmr1.Enabled = True
            MsgBox("Random Move start")
        End If
    End Sub

    Private Sub myMap_OnMapZoomChanged() Handles myMap.OnMapZoomChanged
        lblInfo.Text = "Info : Lat= " + CStr(copter_lat) + ", Lon= " + CStr(copter_lon) + ", Zoom Level= " + CStr(myMap.Zoom)
    End Sub

    Private Sub update_map()
        copter_marker.Position = New PointLatLng(copter_lat, copter_lon)
        myMap.UpdateMarkerLocalPosition(copter_marker)
        myMap.Invalidate()
        lblInfo.Text = "Info : Lat= " + CStr(copter_lat) + ", Lon= " + CStr(copter_lon) + ", Zoom Level= " + CStr(myMap.Zoom)
    End Sub
End Class

Script to check the last date when Bing.com bot is indexing your site

On previous post, I create PHP script to check how many pages from your site is indexed in Bing.com search engine as part of fixing my old SEO statistic checker library. Now, we will check recent date where Bing.com search engine bot indexing your site. We will use scraping technique again, so, we can save our Azure Datamarket quota. How’s this useful for you? Well, it measured how much love Bing.com gave to your site.. 😛

bing.com bot query

Without more talk, here’s the full PHP source code:

//helper function again
//file_get_contents using curl
function file_get_contents_curl($url, $referer="", $ua="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Ubuntu/10.04 Chromium/7.0.514.0 Chrome/7.0.514.0 Safari/534.7")
    {
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Set curl to return the data instead of printing it to the browser.
    if ($referer!="") {
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    } else {
        curl_setopt($ch, CURLOPT_REFERER, $url);
    }
    //curl_setopt($ch, CURLOPT_URL, $url);
    if ($ua!="") {
		curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	} else {
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	}

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $data=curl_exec($ch);
    curl_close ($ch);

    return $data;
}

function bingbot($url)
    {
    $url1                 ="http://www.bing.com/search?q=url%3A" . urlencode($url) . "&go=&qs=n&form=QBRE&mkt=en-WW";
    $data1                =file_get_contents_curl($url1);
    if (strpos($data1, '
", $data2); $xpl4 =explode("", $xpl3[1]); // var_dump($cacheurl, $xpl4); list($mo, $day, $year)=explode('/', $xpl4[0]); return $year . "-" . $mo . "-" . $day; } else { return 'N/A'; } }

Same as before, no fancy coding here. You can improve the data processing by using regular expression. You may ask, why we need to create our own file_get_contents as helper function, PHP already had one? You got it, we can use built-in file_get_contents function if we want. The reason why I create our own file_get_contents is to fool Search Engine that our query is legit and look natural (you know, we are doing illegal thing here –scraping search engine result– 👿 ). The other thing is that cURL has so many options that easily been set to our purpose (sure you can create stream context for built-in file_get_contents but is not that easy to understand to create it) .

How to use it:

echo bingbot('www.mp32u.net');

Example result:

2012-7-23

Fully working demo can be tested on: http://www.vrank.org/ on “Bing.com Bot” section.
As you can see, Bing.com bot is not really accurate. It only mention the date, Googlebot has more detailed info such as the time. Remember that the result might be different depending on your server location (where you run the script).

Script to check how many pages from a site is indexed in Bing.com

OK, this is part of what I’m doing to fix my old script (php pagerank checker and sh*t). I noticed that my Bing.com indexed page checker and Bing.com bot last access checker did not work anymore. It was because I’m not using Bing.com API at all to get the data. Instead, I do  some simple scraping on Bing.com search result page. (So, I’m not calling any Azure Datamarket API here)

Without more a do, this is the full PHP script code:

//helper function
function between($string, $start, $end)
    {
    $string=" " . $string;
    $ini   =strpos($string, $start);

    if ($ini == 0)
        return "";

    $ini+=strlen($start);
    $len=strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
    }

//another helper function
function file_get_contents_curl($url, $referer="", $ua="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Ubuntu/10.04 Chromium/7.0.514.0 Chrome/7.0.514.0 Safari/534.7")
    {
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Set curl to return the data instead of printing it to the browser.
    if ($referer!="") {
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    } else {
        curl_setopt($ch, CURLOPT_REFERER, $url);
    }
    //curl_setopt($ch, CURLOPT_URL, $url);
    if ($ua!="") {
		curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	} else {
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	}

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $data=curl_exec($ch);
    curl_close ($ch);

    return $data;
    }

//this is the main function
function msn_indexed($uri, $badge = 0)
    {
    $uri =trim(str_ireplace('http://', '', $uri));
    $uri =trim(str_ireplace('http', '', $uri));
    $url ='http://www.bing.com/search?q=site%3A' .urlencode( $uri).'&go=&qs=n&sk=&form=QBLH&mkt=en-WW';
    $data=file_get_contents_curl($url);
    
    if (strpos($data, 'sb_count')!==FALSE) {
	return (integer)str_replace(",", "", trim(between($data, '', 'result')));
    } else {
	return 0;
    }
}

No fancy and advanced code there, just simple cut and grab. You might consider using regex when parsing the search results from Bing.com

How to use it

$dx=msn_indexed("www.ahowto.net");

The result would be in integer (0 if Bing.com hasn’t indexed any of your site’s pages).

Fully working demo can be seen here: http://www.vrank.org on “Bing.com Indexed” part.

bing indexed page in search result

this is how you check how many your site’s pages are indexed by Bing.com

As you can see that the result may vary depending on your location (or where you put the script) and sometimes, Bing.com gave invalid result (such as 0, where the real value might be higher than that)

Check your Windows Azure Datamarket remaining quota

In my previous post, I show you how to use Windows Azure Datamarket to create a Bing Search Engine Position checker. I told you that for free account, you only have 5000 requests per month. Paid subscription has higher limit.
This time I’ll show you how to check your remaining monthly quota for all of your Windows Azure Datamarket subscription without logging in to Azure Datamarket. This is the main function:

error_reporting(E_ALL^E_NOTICE);

function check_bing_quota($key) {
	$ret=array();
	
	$context = stream_context_create(array(
		'http' => array(
		    'request_fulluri' => true,
		    'header'  => "Authorization: Basic " . base64_encode($key . ":" . $key)
			)
		)
	);
	
	$end_point='https://api.datamarket.azure.com/Services/My/Datasets?$format=json';
	
	$response=file_get_contents($end_point, 0, $context);
		
	$json_data=json_decode($response);
	
	foreach ($json_data->d->results as $res) {
		$ret[]=array(
			'title'=>$res->Title,
			'provider'=>$res->ProviderName,
			'entry_point'=>$res->ServiceEntryPointUrl,
			'remaining'=>$res->ResourceBalance
		);
	}
	
	return $ret;
}

How to call it:

$ret=check_bing_quota('put_your_real_account_key_here');

Example result:

array(2) {
  [0]=>
  array(4) {
    ["title"]=>
    string(36) "Bing Search API – Web Results Only"
    ["provider"]=>
    string(4) "Bing"
    ["entry_point"]=>
    string(57) "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb"
    ["remaining"]=>
    int(4981)
  }
  [1]=>
  array(4) {
    ["title"]=>
    string(15) "Bing Search API"
    ["provider"]=>
    string(4) "Bing"
    ["entry_point"]=>
    string(54) "https://api.datamarket.azure.com/Data.ashx/Bing/Search"
    ["remaining"]=>
    int(4997)
  }
}

Fully working demo: http://demo.ahowto.net/bing_quota/

check remaining quota in Windows Azure Datamarket

example output formatted as table

Notes:

  1. The function will check ALL of your Windows Azure Datamarket subscription
  2. checking your quota does not subtract your quota
  3. your PHP version must be at least 5.2 and PHP-openssl module must be activated

[php] New Bing SERP checker using Windows Azure Datamarket

So, I got email from Bing Developer Team informing that Bing Search API 2.0 will be gone and moving to new platform: Windows Azure Datamarket. On my previous Bing SERP checker project, I’m using Bing Search API 2.0 which means that those codes will no longer working after 1st August.

Windows Azure Datamarket provide a trial package and a free package that has limit for 5000 requests per month. More than enough for us to toying for Bing SERP checker.

Go get your Account Key (was Application ID) : https://datamarket.azure.com/

This is the revised main function php codes so it will work on new Windows Azure Datamarket web service

error_reporting(E_ALL^E_NOTICE);
    
//this is the main function
function b_serp($keyword, $site, $market, $api_key)
{
	$found=FALSE;
	$theweb='';
	$pos=0;
	$ret=array();
	$limit=1;
	
	$site=str_replace(array('http://'), '', $site);
	
	$context = stream_context_create(array(
		'http' => array(
		    'request_fulluri' => true,
		    'header'  => "Authorization: Basic " . base64_encode($api_key . ":" . $api_key)
			)
		)
	);

	
	$pos=1;
	while ((!$found)&&($pos<=100)) {
		$skip=($limit-1)*50;
		
		//this is the end point of microsoft azure datamarket that we should call  -- only take data from web results
		$end_point='https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query='.urlencode("'".$keyword."'").'&Market='.urlencode("'".$market."'").'&$format=JSON&$top=50&$skip='.$skip;
		
		//I'm using generic file_get_contents because cURL CURLOPT_USERPWD didn't work (no idea why)
		$response=file_get_contents($end_point, 0, $context);
		
		$json_data=json_decode($response);
		
		foreach ($json_data->d->results as $res) {
			$theweb=parse_url($res->Url);

			if (substr_count(strtolower($theweb['host']), $site))
		        {
				$found=TRUE;
				$ret['position']=$pos;
				$ret['title']=$res->Title;
				$ret['url']=$res->Url;
		                return $ret;
		        }

			$pos++;
		}
		
		$limit++;
	}
	
	if (!$found)
	{
		return NULL;
	}
}

How you call it:

$account_key='put your account key here';
$res=b_serp('how to upload mp3 to youtube', 'mp32u.net', 'en-US', $account_key);

Example output:

array(3) {
  ["position"]=>
  int(3)
  ["title"]=>
  string(70) "MP32U.NET - Helping independent artist getting acknowledged in the ..."
  ["url"]=>
  string(21) "http://www.mp32u.net/"
}

Fully working demo: http://demo.ahowto.net/new_bserp/

Actually, there are a lot of other “Search Market” that supported by Bing but I only listed some of them just for example. Download this document for complete list of Bing’s supported Search Market: http://www.bing.com/webmaster/content/developers/ADM_SCHEMA_GUIDE.docx

Notes:

  1. Your PHP version must be PHP 5.2 or higher (because of json_decode command)
  2. You must activate PHP OpenSSL module (php_openssl) because we are using file_get_contents and the webservice end point must be accessed via HTTPS

Update (2012-07-20) : the endpoint URL is changed