你好,游客 登录 注册 搜索
背景:
阅读新闻

Android网络-GoogleMap之GPS定位

[日期:2012-05-02] 来源:Linux社区  作者:superjunjin [字体:大 中 小]
主布局

让map可点击和拖拽 Android:clickable="true"
获取得到的key           android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"

  1. <strong><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.  <com.google.android.maps.MapView  
  8.      android:id="@+id/mapview"  
  9.      android:layout_width="fill_parent"  
  10.      android:layout_height="fill_parent"  
  11.      android:clickable="true"  
  12.      android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"  
  13.      />  
  14.   
  15. </LinearLayout></strong>
主activity
  1. <strong>package com.song;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.google.android.maps.GeoPoint;  
  6. import com.google.android.maps.MapActivity;  
  7. import com.google.android.maps.MapController;  
  8. import com.google.android.maps.MapView;  
  9. import com.google.android.maps.Overlay;  
  10. import com.google.android.maps.Projection;  
  11.   
  12. import android.app.Activity;  
  13. import android.graphics.Bitmap;  
  14. import android.graphics.BitmapFactory;  
  15. import android.graphics.Canvas;  
  16. import android.graphics.Point;  
  17. import android.location.Location;  
  18. import android.location.LocationListener;  
  19. import android.location.LocationManager;  
  20. import android.os.Bundle;  
  21.   
  22. public class C7_GoogleMapActivity extends MapActivity {  
  23.     /** Called when the activity is first created. */  
  24.     MapView mapview;  
  25.     MapController controller;  
  26.     Location location;  
  27.     LocationManager manager;  
  28.     GeoPoint geoPoint;  
  29.     Bitmap bitmap;  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         mapview=(MapView)findViewById(R.id.mapview);    
  35.         manager=(LocationManager)getSystemService(LOCATION_SERVICE);  
  36.         location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  37.         bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pos);//生成bitmap位图   
  38.      
  39.           
  40.         mapview.setBuiltInZoomControls(true);//实现放大缩小功能   
  41.         manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, new LocationListener() {  
  42.               
  43.             @Override   
  44.             public void onStatusChanged(String provider, int status, Bundle extras) {  
  45.                 // TODO Auto-generated method stub   
  46.                   
  47.             }  
  48.                
  49.             @Override  
  50.             public void onProviderEnabled(String provider) {  
  51.                 // TODO Auto-generated method stub   
  52.                   
  53.             }  
  54.               
  55.             @Override  
  56.             public void onProviderDisabled(String provider) {  
  57.                 // TODO Auto-generated method stub   
  58.                   
  59.             }  
  60.               
  61.             @Override  
  62.             public void onLocationChanged(Location location) {  
  63.                 // TODO Auto-generated method stub   
  64.                 getPoint(location);  
  65.             }  
  66.         });  
  67.    
  68.     }  
  69.     public void getPoint(Location location)  
  70.     {  
  71.          controller=mapview.getController();  
  72.          //注意参数是纬度,经度。E6为10的6次方   
  73. //       GeoPoint geoPoint=new GeoPoint((int)(37.898989*1E6), (int)(122.989898*1E6));   
  74.          geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));  
  75.          //定位到哪个点   
  76.          controller.animateTo(geoPoint);  
  77.          //获得悬浮图层   
  78.          List<Overlay> list = mapview.getOverlays();//获得MapView上原有的overlay对象   
  79.          list.clear();//清除所有的overlay对象   
  80.          list.add(new MyOverLay());//加新获取的overlay对象   
  81.     }  
  82.   
  83.     class MyOverLay extends Overlay  
  84.     {    
  85.         //画标记的方法   
  86.         @Override  
  87.         public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
  88.             // TODO Auto-generated method stub   
  89.             super.draw(canvas, mapView, shadow);  
  90.             Projection projection=mapView.getProjection();  
  91.               
  92.             Point point=new Point();//定义输出的像素点   
  93.             projection.toPixels(geoPoint, point);//地理坐标转为像素坐标   
  94.             //绘制图片   
  95.             //bitmap, left, top, paint   
  96.             canvas.drawBitmap(bitmap, point.x-(bitmap.getWidth()/2), point.y-(bitmap.getHeight()), null);  
  97.         }   
  98.     }  
  99.     @Override  
  100.     protected boolean isRouteDisplayed() {  
  101.         // TODO Auto-generated method stub   
  102.         return false;  
  103.     }  
  104. }</strong>

程序效果


linux
相关资讯       Android编程开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款