/**
* @author rongfzh
* @version 1.0.0
*/
package com.android.fzmap;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.android.fzmap.map.FzLocationManager;
import com.android.fzmap.map.FzLocationManager.LocationCallBack;
import com.android.fzmap.map.LongPressOverlay;
import com.android.fzmap.map.MyItemizedOverlay;
import com.android.fzmap.map.SearchSuggestionProvider;
import com.android.fzmap.utils.CommonHelper;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.mobclick.android.MobclickAgent;
/**
*
* @author rongfzh 2011-6-29
*/
public class FzMapActivity extends MapActivity implements LocationCallBack ,OnClickListener{
/** Called when the activity is first created. */
private final String TAG = "FzMapActivity";
private MapView mapView;
private MapController mMapCtrl;
private View popView;
private Drawable myLocationDrawable;
private Drawable mylongPressDrawable;
private FzLocationManager fzLocation;
private MyItemizedOverlay myLocationOverlay;//我的位置 层
private MyItemizedOverlay mLongPressOverlay; //长按时间层
private List<Overlay> mapOverlays;
private OverlayItem overlayitem = null;
private String query;
public GeoPoint locPoint;
ImageButton loction_Btn;
ImageButton layer_Btn;
ImageButton pointwhat_Btn;
Button search_btn;
public final int MSG_VIEW_LONGPRESS = 10001;
public final int MSG_VIEW_ADDRESSNAME = 10002;
public final int MSG_VIEW_ADDRESSNAME_FAIL = 10004;
public final int MSG_VIEW_LOCATIONLATLNG = 10003;
public final int MSG_VIEW_LOCATIONLATLNG_FAIL = 10005;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
loction_Btn = (ImageButton)findViewById(R.id.loction);
layer_Btn = (ImageButton)findViewById(R.id.layer);
pointwhat_Btn = (ImageButton)findViewById(R.id.pointwhat);
loction_Btn = (ImageButton)findViewById(R.id.loction);
search_btn = (Button)findViewById(R.id.search);
loction_Btn.setOnClickListener(this);
layer_Btn.setOnClickListener(this);
pointwhat_Btn.setOnClickListener(this);
search_btn.setOnClickListener(this);
myLocationDrawable = getResources().getDrawable(R.drawable.point_where);
mylongPressDrawable = getResources().getDrawable(R.drawable.point_start);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
initPopView();
mMapCtrl = mapView.getController();
myLocationOverlay = new MyItemizedOverlay(myLocationDrawable,this, mapView, popView, mMapCtrl);
mLongPressOverlay = new MyItemizedOverlay(mylongPressDrawable,this, mapView, popView, mMapCtrl);
mapOverlays = mapView.getOverlays();
mapOverlays.add(new LongPressOverlay(this, mapView, mHandler, mMapCtrl));
//以北京市中心为中心
GeoPoint cityLocPoint = new GeoPoint(39909230, 116397428);
mMapCtrl.animateTo(cityLocPoint);
mMapCtrl.setZoom(12);
FzLocationManager.init(FzMapActivity.this.getApplicationContext() , FzMapActivity.this);
fzLocation = FzLocationManager.getInstance();
}
private void initPopView(){
if(null == popView){
popView = getLayoutInflater().inflate(R.layout.overlay_popup, null);
mapView.addView(popView, new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT, null,
MapView.LayoutParams.BOTTOM_CENTER));
popView.setVisibility(View.GONE);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onCurrentLocation(Location location) {
Log.d(TAG, "onCurrentLocationy");
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
overlayitem = new OverlayItem(point, "我的位置", "");
mMapCtrl.setZoom(16);
if(myLocationOverlay.size() > 0){
myLocationOverlay.removeOverlay(0);
}
myLocationOverlay.addOverlay(overlayitem);
mapOverlays.add(myLocationOverlay);
mMapCtrl.animateTo(point);
}
/**
* 通过经纬度获取地址
* @param point
* @return
*/
private String getLocationAddress(GeoPoint point){
String add = "";
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1);
Address address = addresses.get(0);
int maxLine = address.getMaxAddressLineIndex();
if(maxLine >= 2){
add = address.getAddressLine(1) + address.getAddressLine(2);
}else {
add = address.getAddressLine(1);
}
} catch (IOException e) {
add = "";
e.printStackTrace();
}
return add;
}
private Address searchLocationByName(String addressName){
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.CHINA);
try {
List<Address> addresses = geoCoder.getFromLocationName(addressName, 1);
Address address_send = null;
for(Address address : addresses){
locPoint = new GeoPoint((int)(address.getLatitude() * 1E6), (int)(address.getLongitude() * 1E6));
address.getAddressLine(1);
address_send = address;
}
return address_send;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_VIEW_LONGPRESS://处理长按时间返回位置信息
{
if(null == locPoint) return;
new Thread( new Runnable() {
@Override
public void run() {
String addressName = "";
int count = 0;
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
addressName = getLocationAddress(locPoint);
Log.d(TAG, "获取地址名称");
//请求五次获取不到结果就返回
if("".equals(addressName) && count > 5){
Message msg1 = new Message();
msg1.what = MSG_VIEW_ADDRESSNAME_FAIL;
mHandler.sendMessage(msg1);
break;
}else if("".equals(addressName) ){
continue;
}else{
break;
}
}
if(!"".equals(addressName) || count < 5){
Message msg = new Message();
msg.what = MSG_VIEW_ADDRESSNAME;
msg.obj = addressName;
mHandler.sendMessage(msg);
}
}
}
).start();
overlayitem = new OverlayItem(locPoint, "地址名称",
"正在地址加载...");
if(mLongPressOverlay.size() > 0){
mLongPressOverlay.removeOverlay(0);
}
popView.setVisibility(View.GONE);
mLongPressOverlay.addOverlay(overlayitem);
mLongPressOverlay.setFocus(overlayitem);
mapOverlays.add(mLongPressOverlay);
mMapCtrl.animateTo(locPoint);
mapView.invalidate();
}
break;
case MSG_VIEW_ADDR