ngày 30-03-2016
GoogleMap map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); |
package tranduythanh.com.learngooglemap;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle;
public class MainActivity extends Activity {
//Khai báo đối tượng Google Map GoogleMap map; //Khai báo Progress Bar dialog để làm màn hình chờ ProgressDialog myProgress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Tạo Progress Bar myProgress = new ProgressDialog(this); myProgress.setTitle("Đang tải Map ..."); myProgress.setMessage("Vui lòng chờ..."); myProgress.setCancelable(true); //Hiển thị Progress Bar myProgress.show(); //Lấy đối tượng Google Map ra: map = ((MapFragment)getFragmentManager(). findFragmentById(R.id.map)).getMap(); //thiết lập sự kiện đã tải Map thành công map.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override public void onMapLoaded() { //Đã tải thành công thì tắt Dialog Progress đi myProgress.dismiss(); } }); map.setMapType(GoogleMap.MAP_TYPE_NORMAL); map.getUiSettings().setZoomControlsEnabled(true); map.setMyLocationEnabled(true); } } |
<uses-feature android:name="android.hardware.location" android:required="true" /> <uses-feature android:name="android.hardware.location.gps" android:required="true" /> |
private void TuiDangODau() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria();
Location lastLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (lastLocation != null) { map.animateCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude())) // Sets the center of the map to location user .zoom(15) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(40) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } } |
protected void onCreate(Bundle savedInstanceState) { //.....
//Thêm dòng lệnh này: TuiDangODau(); } |
private void TuiDangODau() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria();
Location lastLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (lastLocation != null) { LatLng latLng=new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()); map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
CameraPosition cameraPosition = new CameraPosition.Builder() .target(latLng) // Sets the center of the map to location user .zoom(15) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(40) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); //Thêm MarketOption cho Map: MarkerOptions option=new MarkerOptions(); option.title("Chỗ Tui đang ngồi đó"); option.snippet("Gần làng SOS"); option.position(latLng); Marker currentMarker= map.addMarker(option); currentMarker.showInfoWindow(); } } |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<ImageView android:id="@+id/img_drthanh" android:layout_width="400dp" android:layout_height="200dp" android:src="@drawable/common_full_open_on_phone" />
<TextView android:id="@+id/tv_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/tv_lng" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title:" />
<TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="snippet:" />
<TextView android:id="@+id/tv_snippet" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout> |
package tranduythanh.com.learngooglemap;
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.model.Marker;
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask;
public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
//Link url hình ảnh bất kỳ private String url; private GoogleMap map; private Activity context; private boolean isCompleted=false; private Marker currentMarker;
public boolean isCompleted() { return isCompleted; }
public void setCompleted(boolean isCompleted) { this.isCompleted = isCompleted; }
public ImageLoadTask(Activity context, String url,GoogleMap map,Marker currentMarker) { this.context=context; this.url = url; this.map=map; this.currentMarker=currentMarker; }
@Override protected Bitmap doInBackground(Void... params) { try { //Tiến hành tạo đối tượng URL URL urlConnection = new URL(url); //Mở kết nối HttpURLConnection connection = (HttpURLConnection) urlConnection .openConnection(); connection.setDoInput(true); connection.connect(); //Đọc dữ liệu InputStream input = connection.getInputStream(); //Tiến hành convert qua hình ảnh Bitmap myBitmap = BitmapFactory.decodeStream(input); if(myBitmap=null) return null; return myBitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
@Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); //thiết lập Info cho Map khi tải hình hoàn tất map.setInfoWindowAdapter(new MyInfoWindowAdapter(context,result)); //tiến hành hiển thị lên Custom marker option lên Map: currentMarker.showInfoWindow(); } } |
package tranduythanh.com.learngooglemap;
import android.app.Activity; import android.graphics.Bitmap; import android.view.View; import android.widget.ImageView; import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker;
public class MyInfoWindowAdapter implements InfoWindowAdapter {
private Activity context; private Bitmap btmp; public MyInfoWindowAdapter(Activity context,Bitmap result) { this.context=context; this.btmp=result; } @Override public View getInfoContents(Marker arg0) { // Getting view from the layout file info_window_layout View v = this.context.getLayoutInflater().inflate(R.layout.custom_info, null);
// Getting the position from the marker LatLng latLng = arg0.getPosition();
// Getting reference to the TextView to set latitude TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
// Getting reference to the TextView to set longitude TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);
ImageView imgdrthanh=(ImageView) v.findViewById(R.id.img_drthanh);
// Setting the latitude tvLat.setText("Latitude:" + latLng.latitude);
// Setting the longitude tvLng.setText("Longitude:"+ latLng.longitude);
tvTitle.setText(arg0.getTitle()); tvSnippet.setText(arg0.getSnippet()); imgdrthanh.setImageBitmap(btmp); return v; }
@Override public View getInfoWindow(Marker arg0) {
return null; } } |
package tranduythanh.com.learngooglemap;
import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity; import android.app.ProgressDialog; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.os.Bundle;
public class MainActivity extends Activity {
//Khai báo đối tượng Google Map GoogleMap map; //Khai báo Progress Bar dialog để làm màn hình chờ ProgressDialog myProgress; GoogleApiClient googleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Tạo Progress Bar myProgress = new ProgressDialog(this); myProgress.setTitle("Đang tải Map ..."); myProgress.setMessage("Vui lòng chờ..."); myProgress.setCancelable(true); //Hiển thị Progress Bar myProgress.show(); //Lấy đối tượng Google Map ra: map = ((MapFragment)getFragmentManager(). findFragmentById(R.id.map)).getMap(); //thiết lập sự kiện đã tải Map thành công map.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override public void onMapLoaded() { //Đã tải thành công thì tắt Dialog Progress đi myProgress.dismiss(); } }); map.setMapType(GoogleMap.MAP_TYPE_NORMAL); map.getUiSettings().setZoomControlsEnabled(true); map.setMyLocationEnabled(true); //lấy lấy được vị trí cuối cùng:
TuiDangODau(); } private void TuiDangODau() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria();
Location lastLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (lastLocation != null) { LatLng latLng=new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()); map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
CameraPosition cameraPosition = new CameraPosition.Builder() .target(latLng) // Sets the center of the map to location user .zoom(15) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(40) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); //Thêm MarketOption cho Map: MarkerOptions option=new MarkerOptions(); option.title("Chỗ Tui đang ngồi đó"); option.snippet("Gần làng SOS"); option.position(latLng); option.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); Marker currentMarker= map.addMarker(option);
ImageLoadTask imgTask=new ImageLoadTask(this,"https://scontent-a-lax.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/1488744_806006112761224_104751868_n.jpg?oh=18c334e98bdbc3454a0b72be9dc3f7dc&oe=55417543",map,currentMarker); imgTask.execute(); } } } |