Android客户端GPS定位源码

转载 2012年03月28日 10:23:51
AndroidManifest.xml文件配置

<?xml version="1.0"encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

     package="cn.itcast.main"

     android:versionCode="1"

     android:versionName="1.0">

    <application android:icon="@drawable/icon"android:label="@string/app_name">

        <activity android:name=".Main"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

       <uses-library android:name="com.google.android.maps"/>

    </application>

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <uses-permission android:name="android.permission.ACCESS_FIND_LOCATION"/>

</manifest>
Main.java文件

package cn.itcast.main;

import java.util.List;

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 android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Point;

import android.location.Address;

import android.location.Criteria;

import android.location.Geocoder;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.view.MotionEvent;

import android.widget.TextView;

public class Main extends MapActivity{

    private MapController mapController;

    private GeoPoint geoPoint;

    private String msg;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //获得mapview

        MapView mapView=(MapView) this.findViewById(R.id.mapview);

        //地图的显示格式为交通图

        mapView.setTraffic(true);

        //设置可控

        mapView.setClickable(true);

        mapView.setEnabled(true);

        mapView.setBuiltInZoomControls(true);

       

        //得到gps设备的访问

        LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

        //设置gps定位配置

        Criteria criteria=new Criteria();

         //设置显示精度

        criteria.setAccuracy(Criteria.ACCURACY_COARSE);

        //是否获得海拔数据

        criteria.setAltitudeRequired(false);

        //是否获得方向数据

        criteria.setBearingRequired(false);

        //是否允许运营商计费

        criteria.setCostAllowed(true);

        //设置耗电程度

        criteria.setPowerRequirement(Criteria.POWER_LOW);

       

        //获得服务供应商

        Stringprovider=locationManager.getBestProvider(criteria, true);

        //获取上一个定位点

        Locationlocation=locationManager.getLastKnownLocation(provider);

        //获得gps定位坐标信息

        Double latitude=location.getLatitude()*1E6;

        Double longitude=location.getLongitude()*1E6;

        //获得卫星定位点

        geoPoint=new GeoPoint(latitude.intValue(),longitude.intValue());

       

        //获得地图控制器

        mapController=mapView.getController();

        //设置地图显示初始化精度

        mapController.setZoom(12);

        mapController.animateTo(geoPoint);

        //实例化自定义绘图层

        MyOverlay myOverlay=new MyOverlay();

        //mapview添加绘图层

        mapView.getOverlays().add(myOverlay);

        //定义一个finalTextView,以备子类引用

       final TextView textView=(TextView) findViewById(R.id.textview);

       LocationListener locationListener=new LocationListener(){

      

       @Override

       public void onStatusChanged(String provider, int status, Bundle extras) {

          

          

       }

      

       @Override

       public void onProviderEnabled(String provider) {

           // TODO Auto-generatedmethod stub

          

       }

      

       @Override

       public void onProviderDisabled(String provider) {

           // TODO Auto-generatedmethod stub

          

       }

      

       @Override

       public void onLocationChanged(Location location) {

           Double latitude=location.getLatitude()*1E6;

           Double longitude=location.getLongitude()*1E6;

           try {

              //获得精度纬度字符串

              msg = "经度:" + location.getLongitude() + "\n";

              msg += "纬度:" + location.getLatitude() + "\n";

              //根据经纬度获得改点地址信息

              Geocoder gc=new Geocoder(Main.this);

              List<Address> addresses=gc.getFromLocation(latitude, longitude,1);

              if (addresses.size()>0) {

                  //获得地址信息

                  msg+="AddressLine:"+addresses.get(0).getAddressLine(0)+"\n";

                  //获得国家名

                  msg += "CountryName" + addresses.get(0).getCountryName()+"\n";

                  msg += "Locality" + addresses.get(0).getLocality() + "\n";

                  msg += "FeatureName" + addresses.get(0).getFeatureName();

              }

              textView.setText(msg);

           } catch (Exception e) {

              e.printStackTrace();

           }

          

       }

    };

        //注册位置监听器,1秒钟扫描1

       locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);

       

    }

   

    class MyOverlay extends Overlay{

     //保证触控事件不重复操作

     private int count=0;

       @Override

       public boolean draw(Canvas canvas, MapView mapView, boolean shadow,

              long when) {

           //定义画笔

           Paint paint=new Paint();

           paint.setColor(Color.RED);

           //定义屏幕点

           Point screenPoint=new Point();

           //gps点转屏幕点

           mapView.getProjection().toPixels(geoPoint, screenPoint);

           //获得gps标志点图片

           Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.flag);

           //绘制gps点图片

           canvas.drawBitmap(bitmap,screenPoint.x,screenPoint.y, paint);

           //绘制文字说明

           canvas.drawText("当前位置", screenPoint.x, screenPoint.y, paint);

           return super.draw(canvas, mapView, shadow, when);

       }

       @Override

       public boolean onTouchEvent(MotionEvent e, MapView mapView) {

           //定义一个屏幕点

           Point screenPoint=new Point();

           //gps点变成屏幕点

           mapView.getProjection().toPixels(geoPoint, screenPoint);

           //获得触点坐标

           int currentX=(int) e.getX();

           int currentY=(int) e.getY();

           //5030范围内触碰,显示当前经纬度

           if ((currentX-screenPoint.x)>=0&&(currentX-screenPoint.x)<50

                  &&(currentY-screenPoint.y>=0)&&(currentY-screenPoint.y)<30) {

              if (count==0) {

                  new AlertDialog.Builder(Main.this).setMessage(msg)

                  .setPositiveButton("确定",new OnClickListener(){

                     @Override

                     public void onClick(DialogInterface dialog, int which) {

                         count=0;

                     }

                    

                  }).show();

              }

              count++;

           }

           return super.onTouchEvent(e, mapView);

       }

    

    }

   

   

    @Override

    protected boolean isRouteDisplayed() {

       // TODO Auto-generatedmethod stub

       return false;

    }

}
1:如果你的手机上没有gps定位系统,会有以下错误
08-10 13:04:55.610:ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to start activityComponentInfo{cn.itcast.main/cn.itcast.main.Main}: java.lang.IllegalArgumentException:provider==null
2:记得去google官网
http://code.google.com/intl/zh-CN/android/maps-api-signup.html
申请自己机器的apikey
C:\Documents and Settings\Administrator\.android>keytool -list-keystore debug.kEystore
默认密码android
我机器的apikey,你用不好使
<com.google.android.maps.MapView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
android:apiKey="0EITsWoXq7NC_mgKW6yIxIViNLhMGsIW4dbmlsg
                 />
最终实现效果:随着android客户端得移动,在地图上提供精确的当前所在位置
举报

相关文章推荐

Android客户端GPS定位源码

AndroidManifest.xml文件配置 xml version="1.0"encoding="utf-8"?> manifest xmlns:android="http://sch...

Android客户端GPS定位源码

AndroidManifest.xml文件配置 xml version="1.0"encoding="utf-8"?> manifest xmlns:android="http://schem...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

【代码】Android 客户端GPS定位源码

AndroidManifest.xml文件配置

最新基于高德地图的android进阶开发(4)GPS定位之POI(Point of interesting)兴趣点源码简介

1.跑了一下高德提供的POI的源码,感觉还是比较简单的,没有达到想要的效果,还有改进的空间,后面会继续更新。2.下面直接上代码,已经注释好了main.javapackage com.dragon.ar...

android GPS定位和卫星个数(源码)

package com.pdager.gps; import java.util.ArrayList; import java.util.Iterator; import java.ut...

Android的基站、WIFI、GPS定位集合【源码】

集合了gps、wifi、基站定位。 其中GPS定位首先是GpsTask类异步返回GPS经纬度信息 ...

android GPS定位和卫星个数(源码)

package com.pdager.gps; import java.util.ArrayList; import java.util.Iterator; import java.util.Lis...
  • swqqcs
  • swqqcs
  • 2012-02-09 15:20
  • 7096
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)