Android: Hiển thị Zoom View trong Google Maps

Android: Hiển thị Zoom View trong Google Maps

Ở bài 1 chúng ta đã hiển thị được Google Maps trên ứng dụng. 1 chức năng tất yếu của Maps là zoom in và zoom out, trong bài này chúng ta sẽ thêm chức năng này vào ứng dụng Google Maps của mình.

Lưu ý : Bạn vẫn tiếp tục sử dụng project ở bài 1.

Đầu tiên , trong Layout main.xml bạn thêm 1 LinearLayout để chứa ZoomControl:

?
1
2
3
4
5
6
7
<LinearLayout android:id ="@+id/zoom"
    android:layout_width ="wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentBottom = "true"
    android:layout_centerHorizontal = "true"
    />

File main.xml của bạn sẽ như sau :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />   
    <com.google.android.maps.MapView android:id="@+id/myMap"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0tl5xRhVSFoXlIq3NfZ97l4zLTKLVHtklJ-D5CQ"
        android:layout_x="2px"
        android:layout_y="100px"
        android:clickable="true" />
    
    <LinearLayout android:id ="@+id/zoom"
        android:layout_width ="wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentBottom = "true"
        android:layout_centerHorizontal = "true"
        />
        
</RelativeLayout>

Trong GoogleMapsActivity.java bạn thêm vào đoạn code sau để hiển thị Zoom Control

?
1
2
3
4
5
6
7
//Add zoom view to Google Maps
mapView = (MapView)findViewById(R.id.myMap);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);

Như các bạn thấy, ta phải khai báo đối tượng mapView của lớp MapView để điều khiển GoogleMaps, và phương thức :

?
1
View zoomView = mapView.getZoomControls();

để trả lại View của ZoomControl.

Full sourcecode của lớp GoogleMapsActivity.java :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.vietandroid.tut.map;
 
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
 
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
 
public class GoogleMapsActivity extends MapActivity {
 
    MapView mapView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Add zoom view to Google Maps
        mapView = (MapView)findViewById(R.id.myMap);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
        View zoomView = mapView.getZoomControls();
        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);
    }
 
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

Chạy chương trình :

Download SourceCode

Nếu muốn bổ sung thêm phím tắt để dùng chức năng ZoomIn và ZoomOut , các bạn có thể Override phương thức onKeyDown() để bắt sự kiện khi bấm phím:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    
    MapController mc = mapView.getController();
    switch (keyCode) {
    case KeyEvent.KEYCODE_I:
            mc.zoomIn();
            break;
    case KeyEvent.KEYCODE_O:
            mc.zoomOut();
            break;
    }
    return super.onKeyDown(keyCode, event);
}

Như trên mình sử dụng phím I để zoomIn, phím O để zoomOut.

Download SourceCode : Clickhere

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Phần 3 tạo Images responsive của Responsive Theme trong Drupal 7

Phần 3 tạo Images responsive của Responsive Theme trong Drupal 7

The recipe also details steps to create a quick responsive image gallery and a responsive banner slideshow.

Trao đổi thân xác ở Cannes - Đổi tình lấy vai diễn và nhận thêm tiền (Phần 3)

Trao đổi thân xác ở Cannes - Đổi tình lấy vai diễn và nhận thêm tiền (Phần 3)

Vết nhơ tình dục tiếp theo vụ "tú ông" Elie Nahas trong khuôn khổ Liên hoan phim Cannes là nhà sản xuất phim Harvey Weinstein người Mỹ (năm nay 67 tuổi)

Facebook

Những hoạt động nào khiến chúng ta nghiện Facebook?

Theo các thống kê, Facebook hiện đang có 901 triệu tài khoản (tính đến tháng 3 năm 2012), với hơn 500 triệu tài khoản hoạt động thường xuyên. Đó là một con số ấn tượng đối với một mạng xã hội thành lập vào năm 2004.

Công ty diệt chuột T&C

 

Diet con trung