百度地圖Overlay

package com.lzz.asfp;android

 

public class PetroleumServerActivity extends Activity implements OnClickListener {git

/**
* MapView 是地圖主控件
*/canvas

private MapView mMapView;
private BaiduMap mBaiduMap;
private Marker mMarker;
private Marker mMarker1;
private InfoWindow mInfoWindow;
int position;
private TextView myOrder;
private LinearLayout back;網絡

double d1;
double d2;app

private String lat = "", lng = "";ide

String imageUrl;
// 初始化全局 bitmap 信息,不用時及時 recycle
BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.drawable.jiayouicon);函數

BitmapDescriptor bdGround = BitmapDescriptorFactory.fromResource(R.drawable.dingwei);ui

private HashMap<String, String> map;
private List<OilStationInfo> info = new ArrayList<>(); // 全部Overlay點位信息this

private int image = 0;url

@SuppressLint("HandlerLeak")
Handler h = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
initOverLay();
}
};
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_petroleum_server);
LocationApplication application = (LocationApplication) getApplication();
application.addOilActivity(this);

// 百度定位轉換爲GPS定位
BDLocation location = new BDLocation();
location.setLatitude(Double.valueOf(NetWorkUtils.getLocLatitude(this)));
location.setLongitude(Double.valueOf(NetWorkUtils.getLocLongitude(this)));

BDLocation location1 = GlobalTool.BAIDU_to_WGS84(location);
lat = location1.getLatitude() + "";
lng = location1.getLongitude() + "";

back = (LinearLayout) findViewById(R.id.back);
initData();
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
myOrder = (TextView) findViewById(R.id.my_order);
myOrder.setOnClickListener(this);
back.setOnClickListener(this);
// 構造一個地圖函數的msu對象,設置對象的縮放等級問14.0,最後設置地圖狀態。
// MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
// mBaiduMap.setMapStatus(msu);
LatLng l = new LatLng(Double.valueOf(NetWorkUtils.getLocLatitude(this)),
Double.valueOf(NetWorkUtils.getLocLongitude(this)));
MapStatus mStatus = new MapStatus.Builder().target(l).build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mStatus);
// 改變地圖狀態
mBaiduMap.setMapStatus(mMapStatusUpdate);

}

public void initOverLay() {

 

//循環加入每一條信息

// add ground overlay
d1 = Double.valueOf(NetWorkUtils.getLocLatitude(this));
d2 = Double.valueOf(NetWorkUtils.getLocLongitude(this));
LatLng lll = new LatLng(d1, d2);
OverlayOptions ooAa = new MarkerOptions().position(lll).icon(bdGround).zIndex(9).draggable(true);
mMarker1 = (Marker) (mBaiduMap.addOverlay(ooAa));
Bundle bundle1 = new Bundle();
bundle1.putString("store_id", "1");
bundle1.putString("store_name", "");
mMarker1.setExtraInfo(bundle1);

if (null != info) {
if (image < info.size()) {
new Thread(new Runnable() {
@Override
public void run() {
while (image < info.size()) {
// 進行網絡加載圖片(放到地圖上的圖標)
if (!"".equals(info.get(image).getLogo_img_url())
&& null != info.get(image).getLogo_img_url()) {
imageUrl = info.get(image).getLogo_img_url();

HttpGet httpRequest = new HttpGet(imageUrl);
// 取得HttpClient 對象
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
// 請求httpClient ,取得HttpRestponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

// 取得相關信息 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
// 得到一個輸入流
InputStream is = httpEntity.getContent();
System.out.println(is.available());
System.out.println("Get, Yes!");
Bitmap bitmap = BitmapFactory.decodeStream(is);
// Bitmap bitmap3 = drawBg4Bitmap(getResources().getColor(R.color.green),bitmap2);
// Bitmap bitmap = BitmapFillet.fillet(bitmap3, 30, 3);

if (is != null) {
bdA = BitmapDescriptorFactory.fromBitmap(bitmap);
if (image < info.size()) {

LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
Double.valueOf(info.get(image).getLng()));
// //GPS轉百度
// 將GPS設備採集的原始GPS座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待轉換座標
converter.coord(desLatLng1);
LatLng desLatLng = converter.convert();
double la = desLatLng.latitude;
double ln = desLatLng.longitude;
LatLng ll = new LatLng(la, ln);
OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA)
.zIndex(9).draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
Bundle bundle = new Bundle();
bundle.putString("store_id", info.get(image).getStore_id());
bundle.putString("store_name", info.get(image).getStore_name());
mMarker.setExtraInfo(bundle);
image++;
} else {
return;
}
}

is.close();
// iv.setImageBitmap(bitmap);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
bdA = BitmapDescriptorFactory.fromResource(R.drawable.moren);
if (image < info.size()) {

LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
Double.valueOf(info.get(image).getLng()));
// //GPS轉百度
// 將GPS設備採集的原始GPS座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待轉換座標
converter.coord(desLatLng1);
LatLng desLatLng = converter.convert();
double la = desLatLng.latitude;
double ln = desLatLng.longitude;
LatLng ll = new LatLng(la, ln);
OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA).zIndex(9)
.draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
Bundle bundle = new Bundle();
bundle.putString("store_id", info.get(image).getStore_id());
bundle.putString("store_name", info.get(image).getStore_name());
mMarker.setExtraInfo(bundle);
image++;
} else {
return;
}
}

} else {
bdA = BitmapDescriptorFactory.fromResource(R.drawable.moren);
if (image < info.size()) {

LatLng desLatLng1 = new LatLng(Double.valueOf(info.get(image).getLat()),
Double.valueOf(info.get(image).getLng()));
// //GPS轉百度
// 將GPS設備採集的原始GPS座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待轉換座標
converter.coord(desLatLng1);
LatLng desLatLng = converter.convert();
double la = desLatLng.latitude;
double ln = desLatLng.longitude;
LatLng ll = new LatLng(la, ln);
OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA).zIndex(9)
.draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
Bundle bundle = new Bundle();
bundle.putString("store_id", info.get(image).getStore_id());
bundle.putString("store_name", info.get(image).getStore_name());
mMarker.setExtraInfo(bundle);
image++;
} else {
return;
}
}
}

}

}).start();
}

}

mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@SuppressLint("InflateParams")
public boolean onMarkerClick(final Marker marker) {

// 在這裏進行循環遍歷
for (int i = 0; i < info.size(); i++) {

//經過上面Marker添加的ExtraInfo區分點擊的是哪個marker點,傳遞不一樣的信息
if (marker.getExtraInfo().getString("store_id").equals(info.get(i).getStore_id())) {
View view = getLayoutInflater().inflate(R.layout.oil_infowindow_layout, null);
TextView name = (TextView) view.findViewById(R.id.store_name);
TextView address = (TextView) view.findViewById(R.id.store_address);
OnInfoWindowClickListener listener = null;

position = i;
name.setText(info.get(position).getStore_name());
address.setText(info.get(position).getAddress());
listener = new OnInfoWindowClickListener() {
public void onInfoWindowClick() {
// LatLng all = marker.getPosition();
// LatLng llNew = new LatLng(ll.latitude +
// 0.005,
// ll.longitude + 0.005);
// marker.setPosition(llNew);
Intent intent = new Intent(PetroleumServerActivity.this,
PetroleumDetailsActivity.class);
intent.putExtra("store_id", info.get(position).getStore_id());
intent.putExtra("address", info.get(position).getAddress());
intent.putExtra("store_name", info.get(position).getStore_name());
intent.putExtra("lng", info.get(position).getLng());
intent.putExtra("lat", info.get(position).getLat());
intent.putExtra("img", info.get(position).getLogo_img_url());
startActivity(intent);
mBaiduMap.hideInfoWindow();
}
};
LatLng ll = marker.getPosition();
mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(view), ll, -65, listener);
mBaiduMap.showInfoWindow(mInfoWindow);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
mBaiduMap.hideInfoWindow();

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
} else if ("1".equals(marker.getExtraInfo().getString("store_id"))) {
View view = getLayoutInflater().inflate(R.layout.myposition_infowindow_layout, null);
OnInfoWindowClickListener listener = null;
// button.setText(info.get(position).getStore_name()+"\n"+info.get(position).getAddress());
// button.setTextSize(15);
// button.setGravity(Gravity.CENTER_VERTICAL);
listener = new OnInfoWindowClickListener() {
public void onInfoWindowClick() {
mBaiduMap.hideInfoWindow();
}
};
LatLng ll = marker.getPosition();
mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(view), ll, -52, listener);
mBaiduMap.showInfoWindow(mInfoWindow);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
mBaiduMap.hideInfoWindow();

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
break;
}
}
return true;
}
});
}

/**
* 清除全部Overlay
*
* @param view
*/
public void clearOverlay(View view) {
mBaiduMap.clear();
}

/**
* 從新添加Overlay
*
* @param view
*/
public void resetOverlay(View view) {
clearOverlay(null);
// initOverlay();
}

@Override
protected void onPause() {
// MapView的生命週期與Activity同步,當activity掛起時需調用MapView.onPause()
mMapView.onPause();
super.onPause();
}

@Override
protected void onResume() {
// MapView的生命週期與Activity同步,當activity恢復時需調用MapView.onResume()
mMapView.onResume();
super.onResume();
}

@Override
protected void onDestroy() {
// MapView的生命週期與Activity同步,當activity銷燬時需調用MapView.destroy()
mMapView.onDestroy();
super.onDestroy();
// 回收 bitmap 資源
bdA.recycle();
}

 

//網絡請求拿到全部的Overlay信息

public void initData() {

map = new HashMap<>();
map.put("lng", lng);
map.put("lat", lat);
if (NetWorkUtils.isAvailable(PetroleumServerActivity.this)) {
String url = UrlPath.GASSTATIONINFO;
DownData.instance().downDataPost(url, map, new onDownListener() {
@Override
public void getValue(boolean isSuccess, String value) {
if (isSuccess && value != null) {
Gson gson = new Gson();
OilInfoVo oilInfo = gson.fromJson(value, OilInfoVo.class);
if ("0".equals(oilInfo.getErrorCode())) {
info = oilInfo.getData();
h.sendEmptyMessage(1);
} else {
Toast.makeText(PetroleumServerActivity.this, "系統處理異常!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(PetroleumServerActivity.this, "網絡異常!", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(PetroleumServerActivity.this, "網絡異常,請稍後重試!", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.my_order:
Intent intent = new Intent(PetroleumServerActivity.this, MyOilOrderListActivity.class);
startActivity(intent);
break;
case R.id.back:
finish();
break;
default:
break;
}

}

@Override public boolean onTouchEvent(MotionEvent event) { // mBaiduMap.hideInfoWindow(); return super.onTouchEvent(event); } //bitmap繪製背景顏色 public static Bitmap drawBg4Bitmap(int color, Bitmap orginBitmap) { Paint paint = new Paint(); paint.setColor(color); Bitmap bitmap = Bitmap.createBitmap(orginBitmap.getWidth(), orginBitmap.getHeight(), orginBitmap.getConfig()); Canvas canvas = new Canvas(bitmap); canvas.drawRect(0, 0, orginBitmap.getWidth(), orginBitmap.getHeight(), paint); canvas.drawBitmap(orginBitmap, 0, 0, paint); return bitmap; } //bitmap設置圓角 }

相關文章
相關標籤/搜索