10/06/2560

การนำข้อมูลจาก blogger.com มาแสดงในหน้าเว็บด้วย blogger api

หากเรามี blog อยู่ใน blogger.com แล้วเราต้องการให้ข้อมูลใน blogger มาแสดงใน Website ของเรา
เราสามารถใช้ blogger api เพื่อนำข้อมูลจาก blogger มาแสดงในเว็บได้ดังนี้


1. เข้าไปที่ https://developers.google.com/blogger/docs/3.0/using#auth เพื่อขอ key สำหรับนำข้อมูล blog มาแสดงในเว็บ แล้วคลิกี่ API Key


2. คลิกที่ GET A KEY เราจะได้  Key ตามรูป




3. หากเราต้องการ แสดงรายการ post ที่มีใน blogger.com  ของเรา  เราสามารถใช้ REST API
https://www.googleapis.com/blogger/v3/blogs/blogID/posts?key=API Key ซึ่ง response ที่ส่งกลับมาจะเป็นในรูปแบบ JSON

โดย blogID สามารถดูได้จาก หน้าบทความทั้งหมดของ blog  ดังรูป


Code สำหรับแสดง post จาก blogger.com

<?php
   //เรียกใช้ blogger api โดยเก็บข้อมูลของ Blog ไว้ในตัวแปล $content
    $content=file_get_contents("https://www.googleapis.com/blogger/v3/blogs/blogID/posts?key=API Key");
    $json=json_decode($content);//แปลงผลลัพธ์จาก blogger api ให้เป็น JSON Object
    $post=$json->items;//เก็บข้อมูลการ Post ของ blog ไว้ในตัวแปร $post

    for($i=0;$i<count($post);$i++)//วน loop แสดงข้อมูลแต่ละ post
    {
        echo "<h4>".$post[$i]->title."</h4>";//แสดงชื่อหัวข้อของ post
        echo "<h5>วันที่ ".$post[$i]->published."</h5>";//แสดงวันที่แผยแพร่
        echo $post[$i]->content;//แสดงเนื้อหาของโพส
        echo "<hr />";

    }

?>

ผลลัพธ์ที่ได้คือ




9/09/2560

สร้าง Android Application Scan QR Code โดย Android Studio

จากที่เคย แนะนำการสร้าง Mobile Application สำหรับ Scan QR Code โดยใช้ Ionic กับ Cordova ไปแล้ว นั้นในครั้งนี้จะเป็นการสอนการสร้าง Android Application สำหรับ Scan QR Code แบบ  Native โดยใช้ Android Studio

1. เรียกใช้ Library zxing สำหรับ Scan QR Code ใน build.gradle (module app) ตามรูป


2. กำหนด Permission ให้  Application สามารถเรีกใช้งาน กล้องมือถือได้ 


3. สร้าง Main Activity โดยมี Layout เป็น 

<LinearLayout
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<!--TextView สำหรับแสดง text ข้อความสำหรับ ผลลัพธ์ของ QR Code ที่ scan ได้ -->
    <TextView
        android:textSize="48px"
        android:textAlignment="center"
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="QR Code Content" />
<!--ปุ่มสำหรับกดเพื่อ Scan QR Code -->
    <Button
        android:layout_marginTop="20px"
        android:id="@+id/scanBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scan QR/Barcode" />

</LinearLayout>

4. เขียนคำสั่งใน MainActivity.java
  
   TextView txtResult;//สร้าง object txtResult ที่เป็น TextView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //กำหนดให้ txtResult มาจาก TextView ที่มี id เป็น txtResult ใน Layout
        txtResult = (TextView)findViewById(R.id.txtResult);
        //กำหนด ชุดคำสั่ง เมื่อ กดปุ่ม ที่มี id เป็น R.id.scanBtn ใน Layout
        Button buttonIntent = (Button)findViewById(R.id.scanBtn);
        buttonIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //เมื่อกดปุ่ม Scan แล้วจะเปิดหน้า Scan QR Code ที่อยู่ใน class ScanActivity
                Intent intent = new Intent(getApplicationContext(),ScanActivity.class);
                startActivityForResult(intent,999);

            }
        });


    }
    //method ที่จะรับข้อความที่ Scan ได้จาก QR Code 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {


            if (resultCode == RESULT_OK)
            {
                //รับค่าข้อความที่ Scan ได้จาก QR Code
                String contents = intent.getStringExtra("SCAN_RESULT");
                txtResult.setText("Result : " + contents);//แสดงข้อความที่ได้ Scan ใส่ใน Layout
            }

    }

5. สร้าง Activity ใหม่ชื่อ  ScanActivity  ซึ่ง  class ScanActivity จะ implements ZXingScannerView.ResultHandler ซึ่ง มาจาก https://github.com/dm77/barcodescanner





//import library สำหรับ Scan QR Code
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private ZXingScannerView zXingScannerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);

        scan();//เมื่อเปิด Activity มาจะให้ เรียก Method Scan เลย
    }

    public void scan(){ //method scan สำหรับ เปิดกล้องอ่าน QR Code
        zXingScannerView =new ZXingScannerView(getApplicationContext());
        setContentView(zXingScannerView);
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();

    }

    @Override
    protected void onPause() {
        super.onPause();
        zXingScannerView.stopCamera();
    }


    @Override
    public void handleResult(Result result) {// method ที่จะทำงานเมื่อ scan QR Code แล้ว
        Toast.makeText(getApplicationContext(),result.getText(), Toast.LENGTH_SHORT).show();
        zXingScannerView.stopCamera();

        Intent returnIntent = new Intent();
       //result.getText() คือ string ข้อความที่ได้หลังจาก Scan QRCode
        returnIntent.putExtra("SCAN_RESULT",result.getText());
        //ส่ง string ข้อความ  กลับไปที่ onActivityResult เพื่อแสดงข้อความของ QR Code
        setResult(RESULT_OK,returnIntent);
        finish();

    }
}

ผลลัพธ์ที่ได้คือ


เมื่อรัน App


เมื่อกดปุ่ม Scan QR Code

หลังจาก Scan QR  แล้ว App แสดง string ของ QR Code ตรง result


หากต้องการติดต่อเรียนทำโปรเจคเกี่ยวกับ QR Code สามารถดูข้อมูลได้ที่ www.projectsoft.biz











8/12/2560

การวาดเส้นลงในแผนที่ google map

สำหรับ Application ที่ใช้ google map และมีความจำเป็นที่จะต้องวาดเส้นใน google map ไม่ไม่ว่าจะเป็นการบอกสถานที่
หรือการแสดงเส้นทางที่เดินทางผ่านมา ( GPS Tracking ) ลงใน google map นั้น เราสามารถวาดเส้นได้ดังตัวอย่างต่อไปนี้

<!-- แผนที่ -->
<div id="map_canvas" style="width: 100%; height: 400px;"></div>
<!-- กำหนดความกว้างยาวแผนนที่ -->

<script>

var map;//ประกาศตัวแปรสำหรับ object แผนที่
var mapOptions = { //สร้างรูปแบบแผนที่
                zoom: 17,//ระดับการ zoom 
                center: new google.maps.LatLng(13.827214295610263,100.52855014801025),
               //ตำแหน่งกึงกลางแผนที่
               mapTypeId: google.maps.MapTypeId.ROADMAP //ให้แสดงแบบแผนที่ถนน
}

//วาดแผนที่ที่ <div id="map_canvas" style="width: 100%; height: 300px;"></div> 
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
//mapOptions คือ รูปแบบแผนที่ zoom กึ่งกลาง

//สร้างตัวแปร array เก็บ json object ตำแหน่ง ของเส้นที่จะแสดง
var json = [
         {lat: 13.827214295610263, lng: 100.52855014801025},
         {lat: 13.828115444270011, lng: 100.52851259708405},
         {lat: 13.82850611457026, lng: 100.52838385105133},
         {lat: 13.828693636081475, lng: 100.52819609642029},
         {lat: 13.828818650338372, lng: 100.52801370620728},
         {lat: 13.828933246681515, lng: 100.528324842453},
         {lat: 13.829110350009977, lng: 100.52865743637085}
];

var line = new google.maps.Polyline({ //สร้าง object เส้นของ google map
          path: json,//ข้อมูลเส้นทาง
          strokeColor: '#FF0000',//สีของเส้น
          strokeWeight: 2 //ความหานของเส้น
});

line.setMap(map);//กำหนดให้ แสดงเส้นในแผนที่

</script>

ผลลัพธ์ที่ได้ google map จะแสดงเส้นดังรูป






7/11/2560

การ scroll ( เลื่อนหน้าเว็บ ) โดยใช้ Jquery


สำหรับ Web Application ที่มีความยาวของเนื้อหาที่ผู้ใช้ต้อง เลื่อน scroll ลงมาเพื่อดูเนื้อหาด้านล่าง หรือการกลับขึ้นไปดูเนื้อหาด้านบนซึ่งทำให้ไม่สะดวกต่อผู้ใช้

jquery สามารถแก้ปัญหาได้คือการเลื่อน scroll อัตโนมัติ โดยใช้คำสั่ง

$('html,body').animate({ scrollTop: 400 }, 600 );

{ scrollTop: 400 } คือ ให้แสดงเนื้อที่ตำแหน่ง 400 จากด้านบน
ส่วน  paramether ตัวที่ 2  600 คือ ความเร็วในเลื่อน 600 มิลิวินาที

4/20/2560

การแสดง google map ใน mobile application ( ionic2)

google map นับว่า เป็นส่วนสำคัญสำหรับ Mobile Application ที่ต้องการระบุตำแหน่งสถานที่ต่างๆ ในบทความนี้จะแสดงให้เห็นวิธีการแสดงแผนที่ google map  ใน ionic2 ซึ่งคือ platform การพัฒนา mobile application แบบ cross platform

สำหรับผู้ที่ยังไม่รู้จัก ionic สามาถอ่าน
การติดตั้ง ionic2 
และ
การ link หน้าใน ionic2 


ขั้นตอนการแสดง google map  สำหรับ Mobile Application (ionic)


1.  ในไฟล์ src/pages/index.html ให้ใส่ google map api library ใน tag header

<script src="https://maps.googleapis.com/maps/api/js?key=&sensor=false&language=th"type="text/javascript"></script>

2. ในไฟล์ interface .html ใส่ tag div ภายใต้ <ion-content > สำหรับแสดงแผนที่

<div id="map_div" style="width: 100%;height:100%"></div>

3. หลัง คำสั่ง import  ใส่

declare var google;//เพื่อให้ type script รู้จัก google map

4.  สร้างคำสั่ง วาด แผนที่ ใน ไฟล์ type script ภายใต้ class

public  map: any;//ตัวแปรอ้างอิงค์แผนที่
public marker:any;//ตัวแปรสำหรับหมุดใน google map


 ionViewDidLoad(){ //คำสั่งสร้างแผนที่จะต้องอยู่ภายใต้ ionViewDidLoad 
     

     let latLng = new google.maps.LatLng(13.829,100.528);//กำหนดจุดกึ่งกลางแผนที่

    let mapOptions = {
      center: latLng,//จุดกึ่งกลาง
      zoom: 6,//การซูมที่ระดับ 6
      mapTypeId: google.maps.MapTypeId.ROADMAP//ให้แสดงถนน
    }
 
   //กำหนดให้แสดงแผนที่ใน map_div ( div tag ที่เราสร้างไว้)
    this.map = new google.maps.Map( document.getElementById("map_div"),mapOptions);
 
       //ให้สรา้ง marker บนแผนที่
          this.marker = new google.maps.Marker({
                        position: new google.maps.LatLng(13.829,100.528) ,
        });
        this.marker.setMap(this.map);

        //ให้กึ่งกลางแผนที่อยู่ที่ 13.829,100.528
        this.map.setCenter(new google.maps.LatLng(13.829,100.528));
        this.map.setZoom(6);//ให้แผนที่มีการการซูมที่ระดับ 6


  }





3/29/2560

วิธีสร้างการแจ้งเตือนใน Android โดย Notification และการทำงานซ้ำโดย AlarmManager




คุณสมบัติที่สำคัญอย่างหนึ่งสำหรับ Mobile Aplication คือ การแจ้งเตือน ( Notification ) เช่น แจ้งเตือนการนัดหมาย หรือแจ้งเตือนข่าวสาร ซึ่งหากเราต้องการให้ Application แจ้งเตือน ได้นั้นอาจต้องมีการทำงานซ้ำ เช่น ตรวจสอบว่ามีการ Update ข้อมูลใหม่ที่ Server หรือไม่ทุกๆ 1 นาที ซึ่งเราสามารถใช้ Class AlarmManager ของ Android ได้ และใช้ Class Notification เพื่อแจ้งเตือนได้ โดยมีตัวอย่างดังนี้

1. ในไฟล์  AndroidMainFest.xml  

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

<!-- Class Main -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<!-- หน้าที่จะแสดงเมื่อกดแจ้งเตือน -->
        <activity android:name=".get_activity">
        </activity>

<!-- ให้ สามารถใช้ Class AlarmReceiver สำหรับการแจ้งเตือนได้ -->
        <receiver
            android:name=".AlarmReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NOTIFY" />
            </intent-filter>
        </receiver>

    </application>

2. ในไฟล์ MainActivity.java 
สร้าง PendingIntent  เพื่อกำหนดว่าให้ AlarmReceiver เป็น Class ที่จะทำงานทุกๆ 1 นาที
PendingIntent pendingIntent = 
PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), 0);

กำหนดให้ Class AlarmReceiver ทำงานทุก 1 นาที
AlarmManager alarmManager = 
(AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 60*1000, pendingIntent);//ทำงานทุกๆ 6000 มิลิวินาที=1 นาที

3. สร้าง Class AlarmReceiver คือ Class ที่จะทำงานทุกๆ 1 นาที

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

//extends BroadcastReceiver ให้ สมารถทำงานเมื่อปิด App ได้

public class AlarmReceiver extends BroadcastReceiver

    @Override //method onReceive คือ method หลักที่จะทำงาน
    public void onReceive(Context context, Intent intent) {

//กำหนดให้ get_activity คือหน้าที่จะเปิดเมื่อกดที่การแจ้งเตือน
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, get_activity.class), 0);

        //สร้างการแจ้งเตือน
        Notification notification =
                new NotificationCompat.Builder(context) 
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("แจ้งเตือน")//หัวข้อที่จะแจ้งเตือน
                        .setContentText("www.projectsoft.biz")//ข้อความที่จะแจ้งเตือน
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .build();

       //แสดงการแจ้งเตือนด้านบน
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify(999, notification);

    }
}

เมื่อ รันโปรแกรมจะพบว่าจะมีการแจ้งเตือนทำงานทุกๆ 1 นาที


2/02/2560

สร้าง Mobile Application อ่าน QR Code โดยใช้ ionic2

ตัวอย่างการสร้าง Mobile Application  สำหรับอ่าน QR Code โดยใช้ Ionic2

1. Create ionic2  โปรเจคโดยใช้ คำสั่ง

" ionic start BarcodeProject blank --v2 "

2.  เขาไปใน Folder Project แล้วพิมพ์ คำสั่ง เพิ่ม Plugin barcodescanner

" cd BarcodeProject "
" ionic plugin add phonegap-plugin-barcodescanner "

3.  หากเราต้องการ เรียกใช้ function  การอ่าน QR Code ต้อง ดำเนินการตามนี้

      3.1 ต้อง import Platform โดยใช้คำสั่ง  import { Platform } from 'ionic-angular';
      3.2 ประกาศ object cordova โดยใช้คำสั่ง declare var cordova:any;
      3.3  ถ้าเราต้องการให้ App อ่าน QR Code ให้ใช้คำสั่ง ดังนี้

        this.platform.ready().then(() => {
            cordova.plugins.barcodeScanner.scan((result) => {

              result.text คือ ข้อความที่ได้จาก การอ่าน qr code
             
            }, (error) => {

               กรณี เกิด Error

            });
        });