Android 基于googleAPI 实现gps定位

(转自他人博客)

1.申请Google API Key,参考前面文章

2.实现GPS的功能需要使用模拟器进行经纬度的模拟设置,请参考前一篇文章进行设置

3.创建一个Build Target为Google APIs的项目

4.修改Androidmanifest文件:

XML/HTML代码
  1. view plaincopy to clipboardprint?  
  2. <uses-library android:name="com.google.android.maps" />    
  3. <uses-permission android:name="android.permission.INTERNET"/>    
  4.      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    
  5.      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    
  6.    

5.修改main.xml文件

XML代码
  1. view plaincopy to clipboardprint?  
  2. <com.google.android.maps.MapView    
  3.     android:id="@+id/MapView01"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     android:apiKey="0f8FBFJliR7j_7aNwDxClBv6VW8O12V2Y21W_CQ"/>    
  7.    

注意:这里的apiKey值请相应修改为自己的key值

6.代码清单:

Java代码
  1. view plaincopy to clipboardprint?  
  2. package com.hoo.android.LocationMap;    
  3. import java.io.IOException;    
  4. import java.util.List;    
  5. import java.util.Locale;    
  6. import android.content.Context;    
  7. import android.graphics.Bitmap;    
  8. import android.graphics.BitmapFactory;    
  9. import android.graphics.Canvas;    
  10. import android.graphics.Paint;    
  11. import android.graphics.Point;    
  12. import android.location.Address;    
  13. import android.location.Criteria;    
  14. import android.location.Geocoder;    
  15. import android.location.Location;    
  16. import android.location.LocationListener;    
  17. import android.location.LocationManager;    
  18. import android.os.Bundle;    
  19. import android.widget.TextView;    
  20. import com.google.android.maps.GeoPoint;    
  21. import com.google.android.maps.MapActivity;    
  22. import com.google.android.maps.MapController;    
  23. import com.google.android.maps.MapView;    
  24. import com.google.android.maps.Overlay;    
  25. public class ActivityLocationMap extends MapActivity     
  26. {    
  27.     public MapController mapController;    
  28.     public MyLocationOverlay myPosition;    
  29.     public MapView myMapView;    
  30.         
  31.     public void onCreate(Bundle savedInstanceState) {    
  32.         super.onCreate(savedInstanceState);    
  33.         setContentView(R.layout.main);    
  34.         //取得LocationManager实例    
  35.         LocationManager locationManager;    
  36.         String context=Context.LOCATION_SERVICE;    
  37.         locationManager=(LocationManager)getSystemService(context);    
  38.         myMapView=(MapView)findViewById(R.id.MapView01);    
  39.         //取得MapController实例,控制地图    
  40.         mapController=myMapView.getController();    
  41.         //设置显示模式为街景模式    
  42.         myMapView.setStreetView(true);    
  43.             
  44.         //*************使用系统自带的控件放大缩小视图***************************    
  45.         //取得MapController对象(控制MapView)    
  46.         mapController = myMapView.getController();     
  47.         //设置地图支持设置模式    
  48.         myMapView.setEnabled(true);    
  49.         //设置地图支持点击    
  50.         myMapView.setClickable(true);       
  51.         //设置缩放控制,这里我们自己实现缩放菜单    
  52.         myMapView.displayZoomControls(true);      
  53.         myMapView.setBuiltInZoomControls(true);     
  54.         //*******************************************************************         
  55.         设置设置地图目前缩放大小倍数(从1到21)    
  56.         mapController.setZoom(17);    
  57.         //设置使用MyLocationOverlay来绘图    
  58.         myPosition=new MyLocationOverlay();    
  59.             
  60.         List<Overlay> overlays=myMapView.getOverlays();    
  61.         overlays.add(myPosition);    
  62.         //设置Criteria(标准服务商)的信息    
  63.         Criteria criteria =new Criteria();    
  64.         //*****设置服务商提供的精度要求,以供筛选提供商************************    
  65.         criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的经纬度的精度                
  66.         criteria.setAltitudeRequired(false); //高度信息是否需要提供    
  67.         criteria.setBearingRequired(false);  //压力(气压?)信息是否需要提供    
  68.         criteria.setCostAllowed(false);  //是否会产生费用    
  69.         criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求标准    
  70.         //*****************************************************    
  71.         //取得效果最好的criteria    
  72.         String provider=locationManager.getBestProvider(criteria, true);    
  73.         //得到坐标相关的信息    
  74.         Location location=locationManager.getLastKnownLocation(provider);    
  75.         //更新位置信息    
  76.         updateWithNewLocation(location);    
  77.         //注册一个周期性的更新,3000ms更新一次,0代表最短距离    
  78.         //locationListener用来监听定位信息的改变(OnLocationChanged)    
  79.         locationManager.requestLocationUpdates(provider, 30000,locationListener);    
  80.     }    
  81.         
  82.     //更新位置信息    
  83.     private void updateWithNewLocation(Location location)     
  84.     {    
  85.         String latLongString; //声明经纬度的字符串    
  86.         TextView myLocationText = (TextView)findViewById(R.id.TextView01);    
  87.         //初始化地址为没有找到,便于处理特殊情况    
  88.         String addressString="没有找到地址\n";    
  89.         if(location!=null)    
  90.         {    
  91.             //****************获取当前的经纬度,并定位到目标*************************    
  92.             //为绘制标志的类设置坐标    
  93.             myPosition.setLocation(location);    
  94.             //取得经度和纬度    
  95.             Double geoLat=location.getLatitude()*1E6;    
  96.             Double geoLng=location.getLongitude()*1E6;    
  97.             //将其转换为int型    
  98.             GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());    
  99.             //定位到指定坐标    
  100.             mapController.animateTo(point);    
  101.             //*********************************************************************    
  102.             double lat=location.getLatitude();  //获得经纬度    
  103.             double lng=location.getLongitude();    
  104.             latLongString="经度:"+lat+"\n纬度:"+lng;   //设置经纬度字符串    
  105.                 
  106.            // double latitude=location.getLatitude();    
  107.             //double longitude=location.getLongitude();    
  108.             //根据地理位置来确定编码    
  109.             Geocoder gc=new Geocoder(this,Locale.getDefault());    
  110.             try    
  111.             {    
  112.                 //取得地址相关的一些信息:经度、纬度    
  113.                 List<Address> addresses=gc.getFromLocation(lat, lng,1);    
  114.                 StringBuilder sb=new StringBuilder();    
  115.                 if(addresses.size()>0)    
  116.                 {    
  117.                     Address address=addresses.get(0);    
  118.                     for(int i=0;i<address.getMaxAddressLineIndex()-1;i++)    
  119.                         sb.append(address.getAddressLine(i)).append(",");                         
  120.                         //获得地址sb.append(address.getLocality()).append("\n");    
  121.                         //获得邮编sb.append(address.getPostalCode()).append("\n");    
  122.                         sb.append(address.getCountryName());    
  123.                         addressString=sb.toString();    
  124.                 }    
  125.             }catch(IOException e){}    
  126.         }    
  127.         else    
  128.         {    
  129.             latLongString="没有找到坐标.\n";    
  130.         }    
  131.         //显示    
  132.         myLocationText.setText("您当前的位置如下:\n"+latLongString+"\n"+addressString);    
  133.     }    
  134.     //监听位置信息的改变    
  135.     private final LocationListener locationListener=new LocationListener()    
  136.     {    
  137.         //当坐标改变时触发此函数    
  138.         public void onLocationChanged(Location location)    
  139.         {    
  140.             updateWithNewLocation(location);    
  141.         }    
  142.         //Provider被disable时触发此函数,比如GPS被关闭     
  143.         public void onProviderDisabled(String provider)    
  144.         {    
  145.             updateWithNewLocation(null);    
  146.         }    
  147.         //Provider被enable时触发此函数,比如GPS被打开    
  148.         public void onProviderEnabled(String provider){}    
  149.         //Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数    
  150.         public void onStatusChanged(String provider,int status,Bundle extras){}    
  151.     };    
  152.     //方法默认是true,服务器所知的状态列信息是否需要显示    
  153.     protected boolean isRouteDisplayed()    
  154.     {    
  155.         return false;    
  156.     }    
  157.         
  158.     class MyLocationOverlay extends Overlay    
  159.     {    
  160.         Location mLocation;    
  161.         //在更新坐标,以便画图    
  162.         public void setLocation(Location location)    
  163.         {    
  164.             mLocation = location;    
  165.         }    
  166.         @Override    
  167.         public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)    
  168.         {    
  169.             super.draw(canvas, mapView, shadow);                
  170.             Paint paint = new Paint();    
  171.             Point myScreenCoords = new Point();    
  172.             // 将经纬度转换成实际屏幕坐标    
  173.             GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));        
  174.             mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);    
  175.             //*********paint相关属性设置*********    
  176.             paint.setStrokeWidth(0);//文    
  177.             paint.setARGB(25525500);    
  178.             paint.setStyle(Paint.Style.STROKE);    
  179.             //***********************************    
  180.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_dot);    
  181.             canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);    
  182.             canvas.drawText("您目前的位置", myScreenCoords.x, myScreenCoords.y, paint);    
  183.             return true;    
  184.         }    
  185.     }    
  186. }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值