首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android googlemap定位开发,且可以自由移动位置重新获取定位,地址信息

2012-09-25 
android google地图定位开发,且可以自由移动位置重新获取定位,地址信息??xml version1.0 encodingut

android google地图定位开发,且可以自由移动位置重新获取定位,地址信息

?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/map_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.google.android.maps.MapView android:id="@+id/map_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q" android:clickable="true" android:enabled="true" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" android:paddingBottom="105dip" > <TextView android:id="@+id/map_bubbleText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/location_tips" android:gravity="left|center" android:maxEms="12" android:paddingLeft="12dip" android:paddingRight="10dip" android:text="@string/load_tips" android:textColor="#cfcfcf" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/point_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="30dip" android:src="@drawable/point_start" /> </LinearLayout> </FrameLayout></LinearLayout>?

import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;public class MyLocationManager {private final String TAG = "FzLocationManager";private static Context mContext;private LocationManager gpsLocationManager;private LocationManager networkLocationManager;private static final int MINTIME = 2000;private static final int MININSTANCE = 2;private static MyLocationManager instance;private Location lastLocation = null;private static LocationCallBack mCallback;public static void init(Context c , LocationCallBack callback) {mContext = c;mCallback = callback;}private MyLocationManager() {// Gps 定位gpsLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);Location gpsLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINTIME, MININSTANCE, locationListener); // 基站定位networkLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);Location networkLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);networkLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,locationListener);}public static MyLocationManager getInstance() {if (null == instance) {instance = new MyLocationManager();}return instance;}private void updateLocation(Location location) {lastLocation = location;mCallback.onCurrentLocation(location);}private final LocationListener locationListener = new LocationListener() {public void onStatusChanged(String provider, int status, Bundle extras) {}public void onProviderEnabled(String provider) {}public void onProviderDisabled(String provider) {}public void onLocationChanged(Location location) {Log.d(TAG, "onLocationChanged");updateLocation(location);}};public Location getMyLocation() {return lastLocation;} private static int ENOUGH_LONG = 1000 * 60; public interface LocationCallBack{/** * 当前位置 * @param location */void onCurrentLocation(Location location);}public void destoryLocationManager(){Log.d(TAG, "destoryLocationManager");gpsLocationManager.removeUpdates(locationListener);networkLocationManager.removeUpdates(locationListener);}}?

import android.view.MotionEvent;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapView;import com.google.android.maps.Overlay;//覆盖整个地图捕捉触控事件的OverLaypublic abstract class MyMapOverlay extends Overlay{private int point_X;private int point_Y;private GeoPoint newPoint;public MyMapOverlay(int x,int y){point_X = x;point_Y = y;}boolean flagMove=false;//触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度 @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) { System.out.println("X->"+event.getX()+":"+point_X); System.out.println("Y->"+event.getY()+":"+point_Y); if(event.getAction() == MotionEvent.ACTION_DOWN){ changePoint(newPoint,1); }else if(event.getAction() == MotionEvent.ACTION_UP){ newPoint = mapView.getProjection().fromPixels(point_X,point_Y); changePoint(newPoint,2); } return false; } public abstract void changePoint(GeoPoint newPoint,int type);}?

import java.io.IOException;import java.util.List;import java.util.Locale;import android.graphics.Rect;import android.location.Address;import android.location.Geocoder;import android.location.Location;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.Window;import android.widget.TextView;import com.android.map.MyLocationManager;import com.android.map.MyLocationManager.LocationCallBack;import com.android.map.MyMapOverlay;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 MyMapActivity extends MapActivity implements LocationCallBack {private MapView mapView;private MapController mMapCtrl;private MyLocationManager myLocation;private List<Overlay> mapOverlays;public GeoPoint locPoint;private MyMapOverlay mOverlay;private TextView desText;private String lost_tips;private int point_X;private int point_Y;private int statusBarHeight;public final int MSG_VIEW_LONGPRESS = 10001;public final int MSG_VIEW_ADDRESSNAME = 10002;public final int MSG_GONE_ADDRESSNAME = 10003;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);mapView = (MapView) findViewById(R.id.map_view);desText = (TextView) this.findViewById(R.id.map_bubbleText);lost_tips = getResources().getString(R.string.load_tips);mapView.setBuiltInZoomControls(true);mapView.setClickable(true);mMapCtrl = mapView.getController();point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2;point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2;mOverlay = new MyMapOverlay(point_X, point_Y) {@Overridepublic void changePoint(GeoPoint newPoint, int type) {if (type == 1) {mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);} else {locPoint = newPoint;mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);}}};mapOverlays = mapView.getOverlays();if (mapOverlays.size() > 0) {mapOverlays.clear();}mapOverlays.add(mOverlay);mMapCtrl.setZoom(12);MyLocationManager.init(MyMapActivity.this.getApplicationContext(),MyMapActivity.this);myLocation = MyLocationManager.getInstance();}@Overrideprotected void onResume() {super.onResume();}@Overrideprotected void onPause() {super.onPause();}@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}public void onCurrentLocation(Location location) {if (locPoint == null) {locPoint = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);}}public void changePoint(GeoPoint locPoint) {}/** * 通过经纬度获取地址 * * @param point * @return */private String getLocationAddress(GeoPoint point) {String add = "";Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());try {List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,1);Address address = addresses.get(0);int maxLine = address.getMaxAddressLineIndex();if (maxLine >= 2) {add = address.getAddressLine(1) + address.getAddressLine(2);} else {add = address.getAddressLine(1);}} catch (IOException e) {add = "";e.printStackTrace();}return add;}/** * * 用线程异步获取 */Runnable getAddressName = new Runnable() {public void run() {String addressName = "";while (true) {addressName = getLocationAddress(locPoint);if (!"".equals(addressName)) {break;}}Message msg = new Message();msg.what = MSG_VIEW_ADDRESSNAME;msg.obj = addressName;mHandler.sendMessage(msg);}};private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息{if (null == locPoint)return;new Thread(getAddressName).start();desText.setVisibility(View.VISIBLE);desText.setText(lost_tips);mMapCtrl.animateTo(locPoint);mapView.invalidate();}break;case MSG_VIEW_ADDRESSNAME:desText.setText((String) msg.obj);desText.setVisibility(View.VISIBLE);if (statusBarHeight == 0) {Rect frame = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);statusBarHeight = frame.top;point_Y -= statusBarHeight / 2;}break;case MSG_GONE_ADDRESSNAME:desText.setVisibility(View.GONE);break;}}};// 关闭程序也关闭定位@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();myLocation.destoryLocationManager();}}?

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!--在application里面添加google地图库如下(一定要记得添加):--><application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="com.google.android.maps" /> <activity android:name="com.android.googlemap.MyMapActivity" android:screenOrientation="portrait" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>?

?

热点排行