1.申请Google API Key,参考前面文章
2.实现GPS的功能需要使用模拟器进行经纬度的模拟设置,请参考前一篇文章进行设置
3.创建一个Build Target为Google APIs的项目
4.修改Androidmanifest文件:
- <uses-library android:name="com.google.android.maps" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
5.修改main.xml文件
- <com.google.android.maps.MapView
- android:id="@+id/MapView01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:apiKey="0f8FBFJliR7j_7aNwDxClBv6VW8O12V2Y21W_CQ"/>
注意:这里的apiKey值请相应修改为自己的key值
6.代码清单:
- package com.hoo.android.LocationMap;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- 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.widget.TextView;
- 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;
- public class ActivityLocationMap extends MapActivity
- {
- public MapController mapController;
- public MyLocationOverlay myPosition;
- public MapView myMapView;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- LocationManager locationManager;
- String context=Context.LOCATION_SERVICE;
- locationManager=(LocationManager)getSystemService(context);
- myMapView=(MapView)findViewById(R.id.MapView01);
-
- mapController=myMapView.getController();
-
- myMapView.setStreetView(true);
-
-
-
- mapController = myMapView.getController();
-
- myMapView.setEnabled(true);
-
- myMapView.setClickable(true);
-
- myMapView.displayZoomControls(true);
- myMapView.setBuiltInZoomControls(true);
-
-
- mapController.setZoom(17);
-
- myPosition=new MyLocationOverlay();
-
- List<Overlay> overlays=myMapView.getOverlays();
- overlays.add(myPosition);
-
- Criteria criteria =new Criteria();
-
- criteria.setAccuracy(Criteria.POWER_HIGH);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(false);
- criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
-
-
- String provider=locationManager.getBestProvider(criteria, true);
-
- Location location=locationManager.getLastKnownLocation(provider);
-
- updateWithNewLocation(location);
-
-
- locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
- }
-
-
- private void updateWithNewLocation(Location location)
- {
- String latLongString;
- TextView myLocationText = (TextView)findViewById(R.id.TextView01);
-
- String addressString="没有找到地址/n";
- if(location!=null)
- {
-
-
- myPosition.setLocation(location);
-
- Double geoLat=location.getLatitude()*1E6;
- Double geoLng=location.getLongitude()*1E6;
-
- GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
-
- mapController.animateTo(point);
-
- double lat=location.getLatitude();
- double lng=location.getLongitude();
- latLongString="经度:"+lat+"/n纬度:"+lng;
-
-
-
-
- Geocoder gc=new Geocoder(this,Locale.getDefault());
- try
- {
-
- List<Address> addresses=gc.getFromLocation(lat, lng,1);
- StringBuilder sb=new StringBuilder();
- if(addresses.size()>0)
- {
- Address address=addresses.get(0);
- for(int i=0;i<address.getMaxAddressLineIndex()-1;i++)
- sb.append(address.getAddressLine(i)).append(",");
-
-
- sb.append(address.getCountryName());
- addressString=sb.toString();
- }
- }catch(IOException e){}
- }
- else
- {
- latLongString="没有找到坐标./n";
- }
-
- myLocationText.setText("您当前的位置如下:/n"+latLongString+"/n"+addressString);
- }
-
- private final LocationListener locationListener=new LocationListener()
- {
-
- public void onLocationChanged(Location location)
- {
- updateWithNewLocation(location);
- }
-
- public void onProviderDisabled(String provider)
- {
- updateWithNewLocation(null);
- }
-
- public void onProviderEnabled(String provider){}
-
- public void onStatusChanged(String provider,int status,Bundle extras){}
- };
-
- protected boolean isRouteDisplayed()
- {
- return false;
- }
-
- class MyLocationOverlay extends Overlay
- {
- Location mLocation;
-
- public void setLocation(Location location)
- {
- mLocation = location;
- }
- @Override
- public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
- {
- super.draw(canvas, mapView, shadow);
- Paint paint = new Paint();
- Point myScreenCoords = new Point();
-
- GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
- mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
-
- paint.setStrokeWidth(0);
- paint.setARGB(255, 255, 0, 0);
- paint.setStyle(Paint.Style.STROKE);
-
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_dot);
- canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
- canvas.drawText("您目前的位置", myScreenCoords.x, myScreenCoords.y, paint);
- return true;
- }
- }
- }
代码参考网络,加以修改优化,谢谢
7.程序运行截图,前提是在命令行下输入geo fix 121.5
31.24(定位到上海东方明珠),在命令行下可以输入其他坐标,系统会根据坐标显示其他位置,如接着输入geo fix 113.325
23.113(定位到广州海心沙),不知为什么输入坐标的时候经常会不识别,有时能够成功而有时不行,郁闷,求解……


Android下实现GPS定位服务,布布扣,bubuko.com
Android下实现GPS定位服务
原文:http://www.cnblogs.com/Free-Thinker/p/3606475.html