Android Google Map : Focus, Zoom Level , Map Type |
Android Google Map : Focus, Zoom Level , Map Type ในหัวข้อที่สองของ Google Map กับ Android เราจะมาเรียนรู้วิธีการเรียกงาน Map ในรูปแบบต่าง ๆ เช่น การ Focus ไปยังตำแหน่งพื้นที่ต่าง ๆ ด้วยค่า Latitude, Longitude และระดับการ Zoom ในแต่ล่ะพื้นที่ รวมทั้งวิธีการแสดงรุปแบบของ Map เช่น ภาพจากดาวเทียม หรือ ภูมิประเทศ และในรูปแบบอื่น ๆ
Android Google Map : Focus, Zoom Level , Map Type
สำหรับตัวอย่างและ 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" />
ออกแบบ Layout ด้วย Flagment

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"/>
Example 1 : การ Zoom และ Focus ไปยัง Location ต่าง ๆ
Syntax
LatLng coordinate = new LatLng(Latitude, Longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
ในการ Zoom เพื่อ Focus ไปยังพื้นที่ต่าง ๆ ด้วยค่า Latitude, Longitude รองรับในแต่ล่ะพื้นที่ต่างกัน โดนค่าที่กำหนดได้จะได้ไม่เกิน 22
ลองมาดู Code เต็ม ๆ
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 android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Latitude > 0 && Longitude > 0)
{
//*** 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));
}
}
}
Screenshot


Example 2 : การเปลี่ยน Map Type หรือมุมมองของ Map แต่ล่ะประเภท

จะเห็นว่ามีอยู่ 4-5 ประเภท
แบบ Terrain
googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_TERRAIN);

แบบ Satellite
googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE);

Example 3 : การเปลี่ยนมุมมองของ Zoom เพื่อย่อยขยาย
ระดับ 13
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 13));

Example 4 : การเปลี่ยนมุมมองในรุปแบบของ 3D (เฉพาะบางพื้นที่)
Syntax
googleMap.setBuildingsEnabled(true);

จะเห็นว่าในเมืองไทยยังไม่มีพื้นที่รองรับ

ในกรณีที่มีพื้นที่รองรับ
Example 5 : การเพื่มปุ่มหรือ Button ย่อย-ขยาย (Zoom In/Out)
Syntax
googleMap.getUiSettings().setZoomControlsEnabled(true);

จะเห็นว่ามีปุ่มสำหรับคลิก Zoom In และ Zoom Out
Code ทั้งหมด
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 android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Latitude > 0 && Longitude > 0)
{
//*** 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, 22));
//*** 3D
googleMap.setBuildingsEnabled(true);
}
}
}

.
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
   |
|
|
Create/Update Date : |
2015-11-21 22:49:32 /
2017-03-26 21:14:01 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|