一个根据GPS信息在地图上的定位的小demo
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:text="经度" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/lng" android:layout_width="85px" android:layout_height="wrap_content" /> <TextView android:text="纬度" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/lat" android:layout_width="85px" android:layout_height="wrap_content" /> <Button android:id="@+id/loc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定位"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"> <RadioButton android:text="普通" android:id="@+id/normal" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="卫星" android:id="@+id/satellite" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/mv" android:clickable="true" android:enabled="true" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0kcrUw1E0GWVCoo6chWBqqxAxiLQGpqgcTII6HQ" /></LinearLayout>
?主界面文件
package com.hc;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.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;public class GooglemapActivity extends MapActivity { /** Called when the activity is first created. */ Button locBn; RadioGroup mapType; MapView mv; EditText etLng, etLat; MapController controller; Bitmap posBitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); mv = (MapView) findViewById(R.id.mv); etLng = (EditText) findViewById(R.id.lng); etLat = (EditText) findViewById(R.id.lat); // 显示放大缩小控制按钮 mv.setBuiltInZoomControls(true); controller = mv.getController(); locBn = (Button) findViewById(R.id.loc); locBn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String lng = etLng.getEditableText().toString().trim(); String lat = etLat.getEditableText().toString().trim(); if (lng.equals("") || lat.equals("")) { Toast.makeText(GooglemapActivity.this, "输入有效经度维度", Toast.LENGTH_LONG).show(); } else { double dlong = Double.parseDouble(lng); double dLat = Double.parseDouble(lat); UpdateMapView(dlong, dLat); } } }); locBn.performClick(); mapType = (RadioGroup) findViewById(R.id.rg); mapType.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.normal: mv.setSatellite(false); break; case R.id.satellite: mv.setSatellite(true); break; } } }); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return true; } private void UpdateMapView(double dlong, double dLat) { GeoPoint gp = new GeoPoint((int)(dLat*1E6), (int)(dlong*1E6)); mv.displayZoomControls(true); controller.animateTo(gp); List<Overlay> ol = mv.getOverlays(); ol.clear(); ol.add(new PosOverLay(gp, posBitmap)); }}
?地图上显示的自定义定位图片
package com.hc;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Point;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapView;import com.google.android.maps.Overlay;import com.google.android.maps.Projection;public class PosOverLay extends Overlay { Bitmap posBitmap; GeoPoint gp; public PosOverLay(GeoPoint gp,Bitmap posBitmap){ super(); this.gp=gp; this.posBitmap=posBitmap; } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { if(!shadow){ Projection proj = mapView.getProjection(); Point p= new Point(); proj.toPixels(gp, p); canvas.drawBitmap(posBitmap, p.x-posBitmap.getWidth()/2,p.y-posBitmap.getHeight(), null); } }}
?manifest文件中加入两条
??? <uses-permission android:name="android.permission.INTERNET"/>? 网络权限
???? <uses-library android:name="com.google.android.maps"/>
?
效果如下
?