package com.example.locationapp;
import java.util.ArrayList;
import java.util.List;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.baidu.mapapi.utils.CoordinateConverter.CoordType;
import com.example.bin.Info;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Point;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
MapView mMapView = null;
private BaiduMap mBaiduMap;
private View popview;
InfoWindow mInfoWindow; //信息图层
//bean类
List
infos;
Info info;
LatLng latlng;
List latLngPolygon;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
//设定中心点坐标
LatLng cenpt = new LatLng(34.75557,113.798519);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenpt)
.zoom(18)
.build();
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
//改变地图状态
mBaiduMap.setMapStatus(mMapStatusUpdate);
//添加多个点,从bean得到数据
info = new Info();
infos = info.infos;
for (int i=0;i
System.out.println("------"+infos.size());
latlng = new LatLng(infos.get(i).getLatitude(), infos.get(i).getLongitude());
System.out.println("========"+latlng);
BitmapDescriptor bitmap2 = BitmapDescriptorFactory.fromResource(R.drawable.location_arrows);
OverlayOptions options2 = new MarkerOptions().position(latlng).icon(bitmap2);
mBaiduMap.addOverlay(options2);
}
//多个标记点的点击事件
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
final LatLng ll = marker.getPosition();
Log.e("标签", "--!" + ll);
Point p = mBaiduMap.getProjection().toScreenLocation(ll);
Log.e("标签", "--!" + p.x + " , " + p.y);
p.y -= 47;
LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);
// (2) //生成一个TextView用户在地图中显示InfoWindow
TextView location = new TextView(getApplicationContext());
location.setBackgroundResource(R.drawable.location_tips);
location.setPadding(5, 5, 5, 5);
location.setTextColor(getResources().getColor(android.R.color.white));
location.setText("5.14-15:32");
mInfoWindow = new InfoWindow(location, ll, -30);
//为弹出的InfoWindow添加点击事件
//显示InfoWindow
mBaiduMap.showInfoWindow(mInfoWindow);
//为弹出的InfoWindow添加点击事件
location.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//隐藏InfoWindow
mBaiduMap.hideInfoWindow();
}
});
return true;
}
});
//绘制折线图层
LatLng latlng1 = new LatLng(34.72557,113.798519);
LatLng latlng2 = new LatLng(34.75457,113.768519);
LatLng latlng3 = new LatLng(34.75357,113.790519);
LatLng latlng4 = new LatLng(34.75657,113.788519);
latLngPolygon = new ArrayList();
latLngPolygon.add(latlng1);
latLngPolygon.add(latlng2);
latLngPolygon.add(latlng3);
latLngPolygon.add(latlng4);
PolylineOptions po = new PolylineOptions().color(getResources().getColor(android.R.color.holo_red_dark)).points(latLngPolygon);
mBaiduMap.addOverlay(po);
// 将GPS设备采集的原始GPS坐标转换成百度坐标
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待转换坐标
converter.coord(latlng1);
LatLng desLatLng1 = converter.convert();
System.out.println("原始:"+latlng1+"转换后:"+desLatLng1);
converter.coord(latlng2);
LatLng desLatLng2 = converter.convert();
converter.coord(latlng3);
LatLng desLatLng3 = converter.convert();
converter.coord(latlng4);
LatLng desLatLng4 = converter.convert();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
}
源码地址:http://download.csdn.net/detail/fei900724/8701271