Android: Các kiểu View và hiển thị một vị trí cụ thể trên Map

Android: Các kiểu View và hiển thị một vị trí cụ thể trên Map

1. Các kiểu View trong Google Maps

Google Maps cung cấp cho chúng ta 3 tùy chọn để xem GoogleMaps là StreetView, SatelliteView và TrafficView.

SatelliteView


TrafficView

StreetView ( hiện tại chỉ hỗ trợ ở 1 số nước Châu Âu vì Google phải sử dụng xe chuyên dụng để chụp ảnh tất cả các tuyến đường nên rất mấy thời gian )

Khi lập trình các bạn muốn view theo kiểu nào thì chỉ cần sử dụng phương thức tương ứng:

?
1
2
3
4
mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.setTraffic(true);

Cụ thể nếu bạn để TrafficView:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
        mapView.setTraffic(true);
    }

2. Xác định vị trí trên Google Maps

Tọa độ 1 điểm trên GoogleMaps được xác định bằng 2 thông số là Kinh độ (Longitude ) và Vĩ độ ( Latitude ). Để hiện thị 1 điểm trên GoogleMaps bạn cần biết được 2 thông số này của vị trí đó.
Cụ thể trong tutorial này mình hiển thị vị trí của Chùa Một Cột lên ứng dụng, đầu tiên bạn phải biết tọa đọa của địa điểm này. Để xác định bạn có thể vào www.maps.google.com để lấy thông số


Như ở trên ta lấy được : Latitude = 21.036074, Longitude= 105.833636 ( Tính bằng đơn vị Độ - Degrees)

Trở lại với Android, ta sử dụng lớp GeoPoint ( com.google.android.maps.GeoPoint ) để chứa tọa độ 1 điểm.

?
1
2
3
4
5
6
GeoPoint p;
String coordinates[] = {"21.036074","105.833636"};
double latiTude = Double.parseDouble(coordinates[0]);
double longiTude = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int)(latiTude * 1E6) ,(int)(longiTude * 1E6));

Do hàm tạo của lớp GeoPoint sử dụng đơn vị là MicroDegrees , nên chúng ta phải nhân thêm 1000000 ( 1E6 ).

Và cuối cùng là show lên map ( ta sử dụng lớp MapController)

?
1
2
3
4
5
6
MapController mc;
mc = mapView.getController();
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();

Phương thức animateTo(GeoPoint p) để điều khiển Google Maps trỏ đến 1 điểm p được định nghĩa trc
Phương thức setZoom(int zoom) để set zoom cho GoogleMaps
Phương thức invalidate() để vẽ lại map của bạn.

Full sourcecode :

?
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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.GeoPoint;
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;
    MapController mc;
    GeoPoint p;
    /** 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);
//        mapView.setSatellite(true);
//        mapView.setStreetView(true);
        mapView.setTraffic(true);
        
        String coordinates[] = {"21.036074","105.833636"};
        double latiTude = Double.parseDouble(coordinates[0]);
        double longiTude = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int)(latiTude * 1E6) ,(int)(longiTude * 1E6));
        
        mc = mapView.getController();
        mc.animateTo(p);
        mc.setZoom(17);
        mapView.invalidate();
        
    }
 
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
 
    @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);
    }
    
    
}

Kết quả :

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

 
Hướng dẫn tạo Static Copy of a Website Drupal

Hướng dẫn tạo Static Copy of a Website Drupal

The modern Web is a dynamic place. However, sometimes it's necessary (or desirable) to remove the dynamic functionality of a website, while preserving its static content.

 
Lợi ích đem lại từ Rich Snippets

Lợi ích đem lại từ Rich Snippets

Chắc hẳn bạn đã từng thấy một số kết quả tìm kiểm hiển thị trên Google được gắn sao khi bạn tìm kiếm với từ khóa nào đó? Đó chính là Rich Snippets. Ở bài viết này chúng tôi sẽ giới thiệu về Rich Snippets và những lợi ích mà nó đem lại.

Máy tính bảng Courier

Bí mật đằng sau vụ Microsoft “khai tử” máy tính bảng Courier

Dù đã bị khai tử hồi tháng 5/2010 nhưng câu chuyện đằng sau “cái chết” bất ngờ và đầy nghi vấn của máy tính bảng Courier vẫn thu hút sự quan tâm của giới công nghệ

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

 

Diet con trung