Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Lp trnh Android

Bi
64

Google Map phn 2

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Ni dung bi hc

Thao tc vi Google Map:


Thay i ch hin th google map
Hin th v tr hin ti ca thit b ln
google Map
Cc thao tc khc: v biu tng

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Thay i ch hin th
private void
xuLyDoiMapType(int position)
google
map

switch (position) {
case 0:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case 1:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case 2:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case 3:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case 4:
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
}

}
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

SupportMapFragment mapFragment = (SupportMapFragment)


getSupportFragmentManager()
.findFragmentById(R.id.map);
V tr hin
GoogleMap mMap = mapFragment.getMap();

mMap.setMyLocationEnabled(true);

ti

private GoogleMap.OnMyLocationChangeListener
myLocationChangeListener = new
GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(),
location.getLongitude());
Marker mMarker = mMap.addMarker(new
MarkerOptions().position(loc));
if(mMap != null){
mMap.animateCamera(
CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
}
};

mMap.setOnMyLocationChangeListener(myLocationChangeListener);
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Tin hnh v cc i tng Shape ln Google Map: Tui s hng dn cch v


Polygons, Circle trn Map, di y l mt s hnh minh ha:

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Polyline,

Lp trnh Android

*polyline:
v v ng polyline (nhng ng thng lin tc nhng khng khp kn)
Ta khai bo i tng PolylineOptions :
PolylineOptions plOption=new PolylineOptions();
Sau a tt c cc ta vo PolylineOptions ri y vo hm :
Polyline polyline= map.addPolyline(plOption);
Hm addPolyline s tr v 1 Polyline, ta c th hiu chnh i tng ny nh sau:
Thit lp mu ng k: polyline.setColor(Color.RED);
Thit lp dy ng k: polyline.setWidth(5)
Thit lp chng lp: polyline.setZIndex(1);

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

*Polygon:
V cc ng line lin tc v khp kn
Tng t ta to i tng : PolygonOptions
PolygonOptions pgOption=new PolygonOptions();
y tt c ta vo PolygonOptions sau a vo hm:
Polygon polyGon= map.addPolygon(pgOption);
Hm addPolygon tr v mt i tng Polygon , ta c th hiu chnh:
Thit lp mu ng vin:polyGon.setStrokeColor(Color.BLUE);
Thit lp mu nn: polyGon.setFillColor(Color.YELLOW);
Thit lp dy: polyGon.setStrokeWidth(5);

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

*Circle:
V ng trn
tng t ta khai bo i tng CircleOptions:
CircleOptions optionCircle=new CircleOptions();
Thit lp ta trung tm v bn knh:
optionCircle.center(latCenter).radius(50);
Sau a vo hm:
Circle cir=map.addCircle(optionCircle);
addCircle tr v mt Circle, ta c th thit lp:
Mu ng vin:cir.setStrokeColor(Color.BLUE);
Mu nn : cir.setFillColor(Color.RED);

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

PolylineO ptions plO ption= n ew PolylineO ptions();


m ap.setO nM apClickListener(n ew O nM apClickListener() {

@ O verride
p u blic void onM apClick(LatLng arg0) {
if(typedraw = = 0)
{
CircleO ptions optionCircle= n ew CircleO ptions();
optionCircle.center(arg0).radius(50);
Circle cir= m ap.addCircle(optionCircle);
cir.setFillColor(Color.RED );
cir.setStrokeColor(Color.BLU E);
if(latPrevious= = n u ll)
latPrevious= arg0;
//xl v ng th
ng
else if(latPrevious!= n u ll)
{
PolylineO ptions optionLine= new PolylineO ptions();
optionLine.add(latPrevious);
optionLine.add(arg0);
Polyline line= m ap.addPolyline(optionLine);
line.setColor(Color.BLU E);
line.setW idth(1);
latPrevious= n u ll;
}

}
else if(typedraw = = 1)
{
if(latPrevious= = n u ll)
plO ption= new PolylineO ptions();

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android
if(latPrevious!= n u ll)
{

plO ption.add(latPrevious);
plO ption.add(arg0);
Polyline polyline= m ap.addPolyline(plO ption);
polyline.setColor(Color.RED );

}
latPrevious= arg0;
}
else if(typedraw = = 2)
{
PolygonO ptions pgO ption= n ew PolygonO ptions();
d ou b le h1= 0.5;
d ou b le h2= 0.25;
LatLng center= arg0;
LatLng p1= new LatLng(center.latitude-h1,center.longitude);
LatLng p2= new LatLng(center.latitude-h2,center.longitude-h2);
LatLng p3= new LatLng(center.latitude-h2,center.longitude-h1);
LatLng p4= new LatLng(center.latitude+ h2,center.longitude-h2);
LatLng p5= new LatLng(center.latitude+ h1,center.longitude-h1);
LatLng p6= new LatLng(center.latitude+ h2,center.longitude);
LatLng p7= new LatLng(center.latitude+ h1,center.longitude+ h1);
LatLng p8= new LatLng(center.latitude+ h2,center.longitude+ h2);
LatLng p9= new LatLng(center.latitude-h2,center.longitude+ h1);
LatLng p10= n ew LatLng(center.latitude-h2,center.longitude+ h2);
pgO ption.add(p1);
pgO ption.add(p2);
pgO ption.add(p3);
pgO ption.add(p4);
pgO ption.add(p5);
pgO ption.add(p6);
pgO ption.add(p7);
pgO ption.add(p8);
pgO ption.add(p9);
pgO ption.add(p10);

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android
pgO ption.add(p1);
Polygon polyG on= m ap.addPolygon(pgO ption);
polyG on.setFillColor(Color.YELLO W );
polyG on.setStrokeColor(Color.BLU E);
polyG on.setStrokeW idth(5);
}

}
});
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

END
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

You might also like