首页 > 代码库 > GoogleMap开发-Android
GoogleMap开发-Android
GoogleMap开发,做个小结,网上很少关于这个的资料,刚学习时,头疼了半天,贴出代码,供大家参考
1、布局文件main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map_layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0.5dip"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
public class MapActivityextends FragmentActivity {
private GoogleMapmap;
private SupportMapFragmentmapFragment;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
initMap();
location(application.getLatitude(),application.getLongitude());
displayAllLocation();
}
private void initControls() {
FragmentManager manager = getSupportFragmentManager();
mapFragment = (SupportMapFragment)manager
.findFragmentById(R.id.map_layout_map);
map = mapFragment.getMap();
}
/**
* Initialize the map
*/
private void initMap() {
try {
map.clear();
map.setMyLocationEnabled(true);
map.setIndoorEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// map.setTrafficEnabled(true);
map.setOnMapLongClickListener(onMapLongClickListener);
// mark视图标记点击事件
map.setOnMarkerClickListener(onMarkerClikcListener);
} catch (Exceptione) {
}
}
/**
* 定位
*
*
*/
publicvoid location(doublelat, double lng) {
try {
LatLng latLng =new LatLng(lat,lng);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.moveCamera(CameraUpdateFactory.zoomTo(10));
} catch (Exceptione) {
CommonHelper.log(this.getClass().getName(),
"searchLocation:" +e.getMessage());
}
}
private void displayAllLocation() {
try {
map.clear();
LatLng at =null;
if (entity.getLat() != 0.0 ||entity.getLng() != 0.0) {
MarkerOptions options =new MarkerOptions();
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.ic_map_loaction);
options.icon(bitmapDescriptor);
LatLng latlng =new LatLng(entity.getLat(),entity.getLng());
options.position(latlng);
options.visible(true);
Marker marker=map.addMarker(options);
at = latlng;
map.moveCamera(CameraUpdateFactory.newLatLng(at));
map.moveCamera(CameraUpdateFactory.zoomTo(10));
}
// map.clear();
} catch (Exceptione) {
CommonHelper.log(this.getClass().getName(),"displayAllLocation:"
+ e.getMessage());
}
}
private OnMarkerClickListener onMarkerClikcListener = new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
MapInforWindow info =new MapInforWindow(MapActivity.this);
map.setInfoWindowAdapter(info);
returnfalse;
}
};
}
3、MapInforWindow
public class MapWOInforWindow implements InfoWindowAdapter{
private Context context;
public MapWOInforWindow(Context context ) {
this.context = context;
}
@Override
public View getInfoContents(Marker arg0) {
return null;
}
@Override
public View getInfoWindow(Marker arg0) {
View view = LayoutInflater.from(context).inflate(
R.layout.map_wo_inforwindow_layout, null);
TextView tvTitle=(TextView)view.findViewById(R.id.map_wo_infowindow_layout_tv_title);
tvTitle.setText("Titlte");
return view;
}
}
4、如果希望进地图页面,直接弹出InforWindow,可以这样写
private void displayAllLocation() {
try {
map.clear();
LatLng at = null;
if (entity.getLat() != 0.0 || entity.getLng() != 0.0) {
MarkerOptions options = new MarkerOptions();
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.ic_map_loaction);
options.icon(bitmapDescriptor);
LatLng latlng = new LatLng(entity.getLat(), entity.getLng());
options.position(latlng);
options.visible(true);
Marker marker=map.addMarker(options);
at = latlng;
map.moveCamera(CameraUpdateFactory.newLatLng(at));
map.moveCamera(CameraUpdateFactory.zoomTo(10));
onMarkerClikcListener.onMarkerClick(marker);//加这句
}
// map.clear();
} catch (Exception e) {
CommonHelper.log(this.getClass().getName(), "displayAllLocation:"
+ e.getMessage());
}
}
private OnMarkerClickListener onMarkerClikcListener = new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
MapInforWindow info = new MapInforWindow(MapActivity.this);
map.setInfoWindowAdapter(info);
arg0.showInfoWindow();//加这句
return false;
}
};
GoogleMap开发-Android