Tag Archives: project

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

Windows Installer Cleaner

Some background:

My SSD drive is almost full, after checking with TreeSize Free, it seems a lot of space is taken by files inside c:\windows\installer\. Searching around, I conclude that some of files is safe to be deleted from that particular place. There is a VB script inΒ http://blogs.msdn.com/b/heaths/archive/2007/01/31/how-to-safely-delete-orphaned-patches.aspx that detect which files that shouldn’t be deleted from c:\windows\installer.

I also know an application named WiCleanup from KZTechs that detect and delete the “safe” files but after I tried it, the confirmation of deletion for files one-by-one makes me uncomfortable. So, I decide, why not programming the application by myself? And this application is born!

Implementation:

I programmed the application in VB .NET language using the free Microsoft Visual Studio 2013 Community Edition. Without further a do, you can download the application here:

[button link=”http://www.ahowto.net/wp-content/uploads/2015/03/Release.zip”]Windows Installer Cleaner Binary Only[/button]
(Of course you need .NET framework 4.5 installed if you want to run this application)
[button link=”http://www.ahowto.net/wp-content/uploads/2015/03/wic_winform.zip”]Windows Installer Cleaner Full Source Code[/button]

How to use:

It’s quite straight forward. There are three buttons there, you just need to click on them sequentially.

  1. Click “Get Kept List” to see which files that you should keep on c:\windows\installer
  2. Click “Get Delete List” to get the files that should be safe to be deleted and their size.
  3. Click “Delete Files” to delete the detected files.

What I learn from building this application:

  • how to create .NET application that need Administrator privilege (UAC things) when executed
  • how to use COM object in .NET application
  • how to use/import .dll from MS Windows in .NET
  • some file operation in .NET (FileInfo, deleting files, file’s attribute)
Windows Installer Cleaner Screenshot in Windows 7 64-bit

Windows Installer Cleaner Screenshot in Windows 7 64-bit

The application is already tested on Windows 8.1 64-bit and Windows 7 SP1 64-bit. Both systems is running fine until now. πŸ˜€

DeAdfly

So, I’ve come to a place(=network) where you can’t access Adf.ly website. I download TV shows a lot, since a lot of TV shows download link is masked/hidden with adf.ly service, I can’t access the real download page because it isn’t redirected properly. Finally I decided to create a PHP script that decode this adf.ly URL into it’s real URL.
Here’s the final result (I know it’s ugly, but it works.. πŸ˜› ):
http://demo.ahowto.net/deadfly/

de-adf.ly
Although the site is created using PHP, I will not share the script. Why? Because a lot of webmasters (including me) gaining revenue from adf.ly, I don’t want this kind of script kill those revenue source. It’s for personal use only.

Script to check if a domain is banned from Adsense program or not

I want to create this kind of script for a long time a go, inspired by Digitalpoint Adsense sandbox tool. Since I have quite a free time now, I decided to do some little research on how to create an Adsense sandbox script. Supposedly to check whether a domain is banned from Google Adsense program or not. After a little research (trial and error), actually this kind of script is not so hard to implement (i.e. once you know the correct URL to call πŸ˜› ).

So, here I create another PHP library that will check whether a domain (the domain name, not the user Adsense account)Β  is banned or not. Here’s the full library source code:

//helper function
//file_get_contents using curl
function file_get_contents_curl($url, $referer="")
    {
    $header_list=array('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
	'Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
	'Accept-Language: en-US,en;q=0.8');

	$ualist=array(
	'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)',
	'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.23 (KHTML, like Gecko) Ubuntu/10.04 Chromium/11.0.688.0 Chrome/11.0.688.0 Safari/534.23',
	'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110221 Ubuntu/10.04 (lucid) Firefox/3.6.14 GTB7.1',
	'Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.01',
	'Midori/0.2.2 (X11; Linux i686; U; en-us) WebKit/531.2+',
	'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0',
	'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1150.1 Iron/20.0.1150.1 Safari/536.11'
	);

	$ch = curl_init();
	//curl_setopt($ch, CURLOPT_HEADER, 0); //not requesting header
	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, $url);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, $ualist[rand(0, count($ualist)-1)]);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header_list);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_TIMEOUT,60);
	$data = curl_exec($ch);
	curl_close($ch);

	return $data;
}

//another helper function
function cut_str($str, $left, $right) {
    $str = substr ( stristr ( $str, $left ), strlen ( $left ) );
    $leftLen = strlen ( stristr ( $str, $right ) );
    $leftLen = $leftLen ? - ($leftLen) : strlen ( $str );
    $str = substr ( $str, 0, $leftLen );
    return $str;
}

//main function
function is_banned($url, $ad_type='text', $format='160x600') {
	$base_url='http://pagead2.googlesyndication.com/pagead/ads?adtest=on&client=ca-skdhfksdhfksdjksjd&random='.rand(1000, 10000000).'&url='.urlencode($url).'&gl=default&ad_type='.$ad_type.'&format='.$format;
	
	$raw=file_get_contents_curl($base_url);
	
	if (strpos($raw, 'id=ads')===FALSE) {
		return TRUE; //is truly banned from Adsense
	} else {
		//get advertisers
		$ads_part=explode(''));
		//var_dump($ads_part);
		$advertisers=array();
		for($i=1;$i<=(count($ads_part)-1);$i++){
			$advertisers[$i-1]=array(
				'url'=>cut_str($ads_part[$i], 'adurl=', '"'),
				'title'=>htmlspecialchars_decode(cut_str($ads_part[$i], '', '')),
				'desc'=>htmlspecialchars_decode(strip_tags(cut_str($ads_part[$i], '
', '
'))) ); } return $advertisers; } }

It’s a very basic PHP code without regular expression with a little cURL added. I’m sure you can improve it. πŸ˜€

How to use it:

$ads=is_banned("mp32u.net");

//check if banned
if (!is_array($ads)) {
    //woops the domain is BANNED
    //do something here
} else {
    //it's not banned
    //you can show possible advertisers URL
    for ($i=0; $i<=count($ads)-1; $i++) {
        //loop through $ads
        //do something, can be shown to table or something
        //$ads[$i]['url'];
        //$ads[$i]['title'];
        //$ads[$i]['desc'];
    }
}  //end if

The function will return an array of possible advertisers for the input URL. If the domain is banned, the function will return TRUE, if not banned, it will return an array.

Example array of advertisers result:

array(2) {
  [0]=>
  array(3) {
    ["url"]=>
    string(28) "http://www2.itt-tech.edu/mgs"
    ["title"]=>
    string(24) "ITT Tech - Official Site"
    ["desc"]=>
    string(62) "Convenient Schedules, Over 130 Locations. Browse New Programs."
  }
  [1]=>
  array(3) {
    ["url"]=>
    string(532) "http://537.xg4ken.com/media/redir.php%3Fprof%3D78%26camp%3D1325%26affcode%3Dcr654548%26cid%3D12788267592%7C491716%7C%26mType%3D%26networkType%3Dcontent%26url%5B%5D%3Dhttp%253A%252F%252Finformation.keller.edu%252Fus%252Fmba-cpa%253Fvc%253D166148%2526agid%253D0078x28874%2526cmpid%253Dctd-Keller-GG%2526utm_source%253Dgoogle%2526utm_medium%253Dcpc%2526utm_term%253D%2526utm_content%253D12788267592%2526utm_campaign%253DAccounting-%252520Content_VC%25253D166148_MT%25253DCTP%2526sc_1%253D0078S001325GGCTP%2526sc_2%253D_kenshoo_clickid_"
    ["title"]=>
    string(22) "Keller Graduate School"
    ["desc"]=>
    string(67) "Prepare For Success w/ Keller's Accounting Master's Degree."
  }
}

Fully working demo can be tested here: http://demo.ahowto.net/adsense_lib/
check if domain is banned by adsense

Close enough to Digitalpoint Adsense Sandbox tool.. πŸ˜›

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).