Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Android Google Map : Search and Find Location

Android Google Map : Search and Find Location ในหัวข้อนี้จะเป็นการเขียน Android App กับ Google Map API เพื่อ Search หรือค้นหาตำแหน่งต่าง ๆ ที่อยู่บน Google Map โดยใช้ชุดคำสั่งคลาสของ Geocoder โดยจะออกแบบหน้าจอให้รับ Input จากผู้ใช้ เมื่อผู้ใช้ทำการค้นหาสถานที่ (Location) จาก Keyword โปรแกรมจะทำการไปค้นหาสถานที่ต่าง ๆ ที่อยู่บน Google Map พร้อมกับปักหมุดและแสดง Tooltip ของ Location นั้น ๆ



Android Google Map : Search and Find Location


สำหรับตัวอย่างและ Code นี้รองรับการเขียนทั้งบนโปรแกรม Eclipse และ Android Studio

ใน AndroidManifest.xml เพิ่ม Permission ดังนี้
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


รูปแบบการ Search and Find Location บน Google Map

Marker Syntax (Multiple)
Geocoder geocoder = new Geocoder(getBaseContext());
addresses = geocoder.getFromLocationName("Search Location", 3);

for(int i=0;i<addresses.size();i++){
	Address address = (Address) addresses.get(i);
	latLng = new LatLng(address.getLatitude(), address.getLongitude());

	//String addressText = String.format("%s, %s",
	//address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
	//address.getCountryName());
}


Example 1 : ตัวอย่างการเขียน Android App เพื่อ Find/Search ค้นหา Location บน Google Map

Android Google Map : Search and Find Location

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search"
            android:layout_alignParentRight="true" />
 
        <EditText
            android:id="@+id/txtKeyword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Input Location"
            android:layout_toLeftOf="@id/btnSearch" />
 
    </RelativeLayout>
 
    <fragment
        android:id="@+id/googleMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
 
</LinearLayout>









MainActivity.java
package com.myapp;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;

public class MainActivity extends FragmentActivity {

	// Google Map
	private GoogleMap googleMap;
	
	// Latitude & Longitude
	private Double Latitude  = 13.844205;
	private Double Longitude  = 100.598856;
	
	// RadioButton
	RadioButton rdoNormal, rdoHybrid, rdoSatellite, rdoTerrain;
	
	LatLng latLng;
	MarkerOptions markerOptions;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);	
		
            //*** Display Google Map
            googleMap = ((SupportMapFragment)getSupportFragmentManager()
                    .findFragmentById(R.id.googleMap)).getMap(); 
            
            LatLng coordinate = new LatLng(Latitude, Longitude);
            
            //*** Focus & Zoom
            googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
            
            //*** Zoom Control
            googleMap.getUiSettings().setRotateGesturesEnabled(true);  
            
            //*** Keyword
            final EditText txtKeyword = (EditText) findViewById(R.id.txtKeyword); 
   
            //*** Button Search
            Button btnSearch = (Button) findViewById(R.id.btnSearch);
            btnSearch.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                	new GeocoderTask().execute(txtKeyword.getText().toString());
                }
            });
            
	}
	
    //*** An AsyncTask Background Process
    private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
 
        @Override
        protected List<Address> doInBackground(String... locationName) {
            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;
 
            try {
                // Getting a maximum of 3 Address that matches the input text
                addresses = geocoder.getFromLocationName(locationName[0], 3);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return addresses;
        }
 
        @Override
        protected void onPostExecute(List<Address> addresses) {
 
            if(addresses==null || addresses.size()==0){
                Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
            }
 
            // Clears all the existing markers on the map
            googleMap.clear();
 
            // Adding Markers on Google Map for each matching address
            for(int i=0;i<addresses.size();i++){
 
                Address address = (Address) addresses.get(i);
 
                // Creating an instance of GeoPoint, to display in Google Map
                latLng = new LatLng(address.getLatitude(), address.getLongitude());
 
                String addressText = String.format("%s, %s",
                address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                address.getCountryName());
 
                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title(addressText);
 
                googleMap.addMarker(markerOptions);
 
                // Locate the first location
                if(i==0)
                {
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                }
            }
        }
    }

}

จาก Code นี้จะทำการค้นหา Location และปักหมุดลงบน Google Map








Screenshot

Android Google Map : Search and Find Location

หน้าจอของ App จะมีช่อง Input สำหรับกรอก Keyword ที่ต้องการ

Android Google Map : Search and Find Location

ค้นหา Location และปักหมุดลงบน Location

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-11-21 22:51:59 / 2017-03-26 21:17:05
  Download : No files
 Sponsored Links / Related

 
Android Google Map (Step by Step)
Rating :

 
Android Google Map : Focus, Zoom Level , Map Type
Rating :

 
Android Google Map : Change Map Type (Normal, Satellite, Hybrid and Terrain)
Rating :

 
Android Google Map : Check Enabled Location Services
Rating :

 
Android Google Map : Get Current Location (Latitude, Longitude)
Rating :

 
Android Google Map : Marker Location (Latitude , Longitude)
Rating :

 
Android Google Map : Adding Multiple Marker (Latitude, Longitude)
Rating :

 
Android Google Map : Marker from Current Location (LocationChanged)
Rating :

 
Android Google Map : Get Address Name from Latitude,Longitude (Geocoder)
Rating :

 
Android Google Map : Marker Location from PHP/MySQL (JSON)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่