Trung tâm Tin học – ĐH KHTN ĐÁNH DẤU MAP Tạo project: Project name: test_location3 Build Target: Android 2.1 Application name: test_location3 Package name: com.dac Create Activity: Test_location3Activity Trong file main.xml : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nhap DD: " /> <EditText android:id="@+id/editDD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" > <requestFocus /> </EditText> <Button android:id="@+id/btnOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tim" /> Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN </LinearLayout> <com.google.android.maps.MapView android:id="@+id/MapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="KEYS" /> </LinearLayout> Tạo thêm 1 class DiaDiem trong package chính : package com.dac; import java.util.ArrayList; import java.util.Iterator; import android.graphics.drawable.Drawable; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class DiaDiem extends ItemizedOverlay { OverlayItem location; private GeoPoint center = null; public DiaDiem(Drawable m, int lat, int lon) { super(m); // TODO Auto-generated constructor stub GeoPoint geo = new GeoPoint(lat, lon); location = new OverlayItem(geo, "Dia diem cua toi", "DD"); populate(); } public GeoPoint getCenterPt() { if(center == null) { int bac = -9000000; int nam = 9000000; int dong = -180000000; int tay = 180000000; GeoPoint pt = location.getPoint(); if(pt.getLatitudeE6() > bac) bac = pt.getLatitudeE6(); if(pt.getLatitudeE6() < nam) Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN nam = pt.getLatitudeE6(); if(pt.getLongitudeE6() > dong) dong= pt.getLongitudeE6(); if(pt.getLongitudeE6()<tay) tay = pt.getLongitudeE6(); center = new GeoPoint((int )((bac+nam)/2), (int)((dong+tay)/2)); } return center; } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return location; } @Override public int size() { // TODO Auto-generated method stub return 1; } } Và trong file Activity chính: package com.dac; import java.io.IOException; import java.util.List; import android.graphics.drawable.Drawable; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.google.android.maps.*; public class Test_location3Activity extends MapActivity { Geocoder geocoder = null; MapView mv = null; Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN DiaDiem fun; Drawable marker; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mv = (MapView)findViewById(R.id.MapView); mv.setBuiltInZoomControls(true); marker = getResources().getDrawable(R.drawable.map_icon1); marker.setBounds((int)(-marker.getIntrinsicWidth()/2), -marker.getIntrinsicHeight(), (int)(marker.getIntrinsicWidth()/2),0); int lat = (int)(10.763238 * 1000000); int lon = (int)(106.675647 * 1000000); GeoPoint pt = new GeoPoint(lat, lon); mv.getController().setZoom(17); mv.getController().setCenter(pt); geocoder = new Geocoder(Test_location3Activity.this); Button btn = (Button)findViewById(R.id.btnOK); btn.setOnClickListener(o); } OnClickListener o = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EditText ed = (EditText)findViewById(R.id.editDD); String Ten = ed.getText().toString(); try { List<Address> l = geocoder.getFromLocationName(Ten, 5); if(l!=null && l.size()>0) { int lat = (int)(l.get(0).getLatitude()*1000000); int lon = (int)(l.get(0).getLongitude()*1000000); GeoPoint pt = new GeoPoint(lat, lon); fun = new DiaDiem(marker, lat, lon); mv.getOverlays().add(fun); mv.getController().setZoom(17); mv.getController().setCenter(pt); } } catch (IOException e) { // TODO Auto-generated catch block Log.v("Loi: ", "Loi 1 cua tui ne"); Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN } } }; @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected boolean isLocationDisplayed() { // TODO Auto-generated method stub return false; } } Hi`nh ảnh của ứng dụng: Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN Lập trình Android – http://laptrinhdidong.vn Page 6 . android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nhap DD: " /> <EditText android:id="@+id/editDD" android:layout_width="wrap_content" android:layout_height="wrap_content" . android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" . xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1"