package com.example.android.apis.view;import com.example.android.apis.MapsDemo;
import com.example.android.google.apis.R;import java.util.List;
import java.util.Map;import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;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 android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MapViewDemo extends MapActivity {
    LocationManager     phonelocationmanager;
    Location            phonelocation;
    Button              getPhonegpsbutton;
    private Location    location           =null;
    MapView map;    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        map = (MapView)findViewById(R.id.map);        
        map.getController().setZoom(10);//缩放10
        map.setSatellite(false);
        map.setBuiltInZoomControls(true);
//画图
        List<Overlay> mapOverlays = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.h_code2);
        CustomItemizedOverlay itemizedOverlay = 
             new CustomItemizedOverlay(drawable, this);
        GeoPoint point = new GeoPoint(22656180,113897460);
//图1的红气球,深圳宝安
        OverlayItem overlayitem =new OverlayItem(new GeoPoint  ((int)(22.656180*1000000), (int)(113.897460*1000000)), "您当前车所在的位置", "1");
      
        
        //图2的红气球,深圳,坐标差不多,都在深圳内
        OverlayItem overlayItem2=new OverlayItem(new GeoPoint((int)(22.676180*1000000), (int)(113.997460*1000000)), "Hello", "2");
       
        itemizedOverlay.addOverlay(overlayitem);
        itemizedOverlay.addOverlay(overlayItem2);
mapOverlays.add(itemizedOverlay);
        map.getController().animateTo(point);
        
        /* 建立location manager 对象 取到系统location服务 */
        phonelocationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        /* 每隔2秒钟监听一次,最小监听距离5米 */
        phonelocationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,5,updateLocationListener);
        getPhonegpsbutton=(Button)findViewById(R.id.getphonegps);
        getPhonegpsbutton.setOnClickListener(new Button.OnClickListener()
          {
            public void onClick(View v)
              {
//判断GPS开启状态
                LocationManager alm=(LocationManager)MapViewDemo.this.getSystemService(Context.LOCATION_SERVICE);
                if(alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
                  {
                   getphoneGPS();
                  }
                else
                  {
                    Toast.makeText(MapViewDemo.this,"请开启GPS!",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Settings.ACTION_SECURITY_SETTINGS);
                    startActivityForResult(intent,0); //设置完成后返回到获取界面
                  }
              }
          });
  }
     @Override
    protected boolean isRouteDisplayed() { return false; }
    
    public final LocationListener updateLocationListener =new LocationListener()
      {
        @Override
        public void onStatusChanged(String provider,int status,Bundle extras)
          {
            // TODO Auto-generated method stub
          }        @Override
        public void onProviderEnabled(String provider)
          {
            // TODO Auto-generated method stub
          }        @Override
        public void onProviderDisabled(String provider)
          {
            // TODO Auto-generated method stub
          }        @Override
        public void onLocationChanged(Location location)
          {
            // TODO Auto-generated method stub
            //更新位置发生变化
            updateLocation(location);
          }
      };
/**
     * 得到手机的gps位置
     */
    private void getphoneGPS()
      {
        updateLocation(location);
        //监听器最小2秒钟监听一次,最小的监听距离为5米
        phonelocation=phonelocationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(phonelocation!=null)
          {
            int lng = (int)(phonelocation.getLongitude()*1000000); //经度
            int lat = (int)(phonelocation.getLatitude()*1000000);  //纬度
//问题出在这里了。我所获取到的经纬度,和红气球2,红气球1都差不多,为什么,红气球1,2都在深圳,
//而我获取到的坐标绘制到google Map里面 怎么就去了非洲了呢????求解答
//诸位可以在DDMS中设置相差不大的经纬度,试验下。
            Drawable phonedrawable = this.getResources().getDrawable(R.drawable.icon);
            CustomItemizedOverlay itemizedOverlay = 
                 new CustomItemizedOverlay(phonedrawable, this);
           
            GeoPoint phonepoint = new GeoPoint((int)phonelocation.getLongitude(),(int)phonelocation.getLatitude());
OverlayItem phoneoverlayitem =new OverlayItem(new GeoPoint(lng, lat), "您当前所在的位置", "1");
            itemizedOverlay.addOverlay(phoneoverlayitem);
List<Overlay> mapOverlays=map.getOverlays();
            mapOverlays.add(itemizedOverlay);
//将GPS获取到的坐标移动到中心
            map.getController().animateTo(phonepoint);
//下行注释代码输出 获取GPS获取坐标的经纬度
           // Toast.makeText(this,"纬度"+x+"经度"+y,Toast.LENGTH_LONG).show();
  }
        else
          Toast.makeText(this,"无法从GPS中获取当前位置信息",Toast.LENGTH_SHORT).show();
          Log.i("phonelocation","phonelocation为空");
      }    /**
     * 更新location
     */
    private void updateLocation(Location location)
      {
        String latLongString;
        if(location!=null)
          {
           // String latLongString;
            int lat=(int)(location.getLatitude()*1000000);
            int lng=(int)(location.getLongitude()*1000000);
            latLongString="维 度:"+lat+"\n经 度: "+lng;
            Toast.makeText(this,"纬度"+lng+"经度"+lat,Toast.LENGTH_LONG).show();
           // Toast.makeText(this,latLongString,Toast.LENGTH_LONG).show();
          }
        else
          {
            latLongString="无法获取地理信息";
          }
      }
  }