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 : Marker from Current Location (LocationChanged)

Android Google Map : Marker from Current Location (LocationChanged) ในหัวข้อนี้เราจะมารู้วิธีการ Marker (ปักหมุด) CUrrent Location (ตำแหน่งปัจจุลัน) ลงใน Google Map ด้วย Android App จะเป็นวิธีการระบุตำแหน่งหรือพิกัด Location ที่เราต้องการะทำเป็นสัญลักษณ์และรุปภาพ โดยตำแหน่งที่จะปักหมุด (Marker) นั้นเราจะอ้างจากค่า Latitude, Longitude ที่ได้จาก locationManager ที่อ้างถึง Location ปัจจุบันของเครื่องที่ติดตั้ง App



Android Google Map : Marker from Current Location (LocationChanged)


สำหรับตัวอย่างและ 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" />


รูปแบบการ Marker Location บน Google Map

locationManager and Marker Syntax
	public void onLocationChanged(Location location) {
		
		if (location == null) { return; }
		
		//** Get Latitude & Longitude
		Latitude = location.getLatitude();
		Longitude= location.getLongitude();

		googleMap.clear();
		
        //*** Focus & Zoom
        LatLng coordinate = new LatLng(Latitude, Longitude);
        googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
        
        //*** Marker
        MarkerOptions marker = new MarkerOptions()
        		.position(new LatLng(Latitude, Longitude)).title("Your current location");
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
        googleMap.addMarker(marker);
	}

ในการ Maker Location บน Google Map สามารถใช้คำสั่ง addMarker(marker) โดยค่า Latitude และ Longitude จะได้จาก locationManager โดยเราจะต้องแทรกไว้ใน method : onLocationChanged

Example 1 : ตัวอย่างการเขียน Android App ปักหมุดหรือ Mark Location จาก Latitude และ Longitude ที่ได้จากตำแหน่ง Location ปัจจุบัน

Android Google Map : Marker from Current Location (LocationChanged)

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/googleMap"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>


MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

public class MainActivity extends FragmentActivity implements LocationListener {

	protected LocationManager locationManager;
	protected LocationListener locationListener;
	
	// Google Map
	private GoogleMap googleMap;	

	// Latitude & Longitude
	private Double Latitude  = 0.00;
	private Double Longitude  = 0.00;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//*** Location 
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
		
        //*** Display Google Map
        googleMap = ((SupportMapFragment)getSupportFragmentManager()
                .findFragmentById(R.id.googleMap)).getMap(); 	
	}

	@Override
	public void onLocationChanged(Location location) {
		
		if (location == null) { return; }
		
		//** Get Latitude & Longitude
		Latitude = location.getLatitude();
		Longitude= location.getLongitude();

		googleMap.clear();
		
        //*** Focus & Zoom
        LatLng coordinate = new LatLng(Latitude, Longitude);
        googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
        
        //*** Marker
        MarkerOptions marker = new MarkerOptions()
        		.position(new LatLng(Latitude, Longitude)).title("Your current location");
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
        googleMap.addMarker(marker);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// Log.d("Latitude","disable");
	}

	@Override
	public void onProviderEnabled(String provider) {
		// Log.d("Latitude","enable");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// Log.d("Latitude","status");
	}

}

Screenshot

Android Google Map : Marker from Current Location (LocationChanged)

แสดงตำแหน่งที่ Marker (ปักหมุด) ปัจจุบัน ของอุปกรณ์ที่เรียกใช้งาน App และหมุดจะเปลี่ยนไปเรื่อย ๆ เมื่ออุปกรณ์ที่เราเปิดนั้นเคลื่อนที่ไปยัง Location หรือ ตำแหน่งอื่น ๆ






   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-11-21 22:51:30 / 2015-11-23 15:12:06
  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 : Get Address Name from Latitude,Longitude (Geocoder)
Rating :

 
Android Google Map : Search and Find Location
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 05
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 อัตราราคา คลิกที่นี่