主要代码如下:

package com.easyway.android.google;import java.io.IOException;import java.util.List;import java.util.Locale;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.location.Address;import android.location.Criteria;import android.location.Geocoder;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapActivity;import com.google.android.maps.MapController;import com.google.android.maps.MapView;/** * 在定位方面可以通过LocationManager.GPS_PROVIDER和LocationManager.NETWORK_PROVIDER两种方式获取定位位置。 * 除了直接使用LocationManager提供的静态Provider(如GPS_PROVIDER和NETWORK_PROVIDER等)外,还可以使用我们自 * 己创建的LocationProvider对象。 *创建LocationProvider对象一般要先创建Criteria对象,来设置我们的LocationProvider要满足什么样的标准 *Criteria myCri=new Criteria(); *myCri.setAccuracy(Criteria.ACCURACY_FINE);//精确度  *myCri.setAltitudeRequired(false);//海拔不需要 *myCri.setBearingRequired(false);//Bearing是“轴承”的意思,此处可理解为地轴线之类的东西,总之Bearing Information *是一种地理位置信息的描述 *myCri.setCostAllowed(true);//允许产生现金消费 *myCri.setPowerRequirement(Criteria.POWER_LOW);//耗电 *String myProvider=locMan.getBestProvider(myCri,true); * *  * 运行成功之后地图定位到当前所在位置 *  *  * 运行须知: *   1.在创建项目时选择包含Google Api的Android SDK *    <com.google.android.maps.MapView *     android:id="@+id/mapView" *     android:layout_width="fill_parent" *     android:layout_height="fill_parent" *     android:enabled="true" *     android:clickable="true" *     android:apiKey="GoogleAPK" *     />   *   2.必须具有访问地图和网络的权限程序需要实时的从Google地图库中读取信息,所以添加接入Internet权限。 *      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> *        <uses-permission android:name="android.permission.INTERNET" />   *   3.在AndroidManifest.xml中配置必须添加香瓜的用于类库userlibary *    <uses-library android:name="com.google.android.maps" />   *     *    例如: *     <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">   *     <uses-library android:name="com.google.android.maps" />     *        <activity android:name=".AndroidGoogleMapActivity"   *                  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>   * 4.AndroidGoogleMapActivity文件。在这个activity中我们继承了一个特别的类叫做:MapActivity 。 *  * * @author longgangbai * */public class AndroidGoogleMapActivity extends MapActivity {   private MapView mapView;      MapController mapController;      /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mapView=(MapView)findViewById(R.id.mapView);        this.mapView.setBuiltInZoomControls(true);//可以多点触摸放大         mapView.setSatellite(true);//使用卫星图           //1.定位地图中要查看的经纬度        GeoPoint point=new GeoPoint(39000000, 116000000);         this.mapController=mapView.getController();         this.mapController.animateTo(point);//通过动画方式移动到指定坐标         Log.i("welcome", "created map activity!");                 //2.自动定位功能功能由于种种原因不可以    //获取定位设置对象//Location location = getLocationManager();        //获取当前的经纬度//getCurrentLocation(location);           //3.根据输入的地址查询相关的地址    queryLocationByAddress("苏州科技城高新软件园");    }        /** * 根据地址查询相关的经纬度 * @param address * @throws IOException  */public void queryLocationByAddress(String locationName) {//根据本地Locale获取地点Locale locale=Locale.getDefault();//创建Geocoder解析地址Geocoder gc=new Geocoder(AndroidGoogleMapActivity.this,locale/* Locale.CHINA */);//Locale是java.util中的一个类    //gc.getFromLocation(latitude, longitude, 1);    List<Address> addressList;try {    addressList = gc.getFromLocationName(locationName, 1);    StringBuilder buffer=new StringBuilder();    if(addressList!=null&&addressList.size()>0){    Address address=addressList.get(0);    for (int i=0;i<address.getMaxAddressLineIndex();i++) {    buffer.append(address.getAddressLine(i)).append(" \n");}    double latitude=address.getLatitude()*1E6;//经度    double longitude=address.getLongitude()*1E6; //纬度    //定位到输入地址    queryAddressByLocation(new GeoPoint((int)latitude, (int)longitude));        //显示该地址的信息    buffer.append("经度为:"+latitude+"纬度为"+longitude).append("\n");    buffer.append(address.getLocality()).append("\n");    buffer.append(address.getPostalCode()).append("\n");    buffer.append(address.getCountryName ()).append("\n");      Log.v("tag", "latitude " + latitude + "  longitude:" + longitude );           //创建对话框          AlertDialog.Builder builder=new Builder(AndroidGoogleMapActivity.this);          builder.setTitle("当前的经纬度")          .setMessage(buffer)          .setPositiveButton("确定", new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  dialog.dismiss();  }  })  .create()  .show();    }}catch (IOException e) {e.printStackTrace();    }}    /**     * 获取定位管理器     * @return     */private Location getLocationManager() {  Criteria mCriteria = new Criteria();          mCriteria.setAccuracy(Criteria.ACCURACY_FINE);          mCriteria.setAltitudeRequired(false);          mCriteria.setBearingRequired(false);          mCriteria.setCostAllowed(true);          mCriteria.setPowerRequirement(Criteria.POWER_LOW);//获取定位管理器        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);         locationManager.getBestProvider(mCriteria, true);        //基于GPS定位的功能的        //GPS功能必须开启,在真机才可以模拟        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        if(location==null){        //采用基于网络的,必须在公共上ip注册的才可以使用此种方式        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);        }return location;}      /** *  * @param latitude * @param longitude */public void queryAddressByLocation(GeoPoint p){//GeoPoint geoGPSLocation = new GeoPoint((int)(m_CurrentLocation.getLatitude()*1000000), (int)(m_CurrentLocation.getLongitude()*1000000));//Android中提供了一个叫ZoomControls的控件对地图来进行放大和缩小处理。mapView.displayZoomControls(true);//显示地图缩放的按钮mapController.animateTo(p);//带动画移到p点mapController.setZoom(7);}/** * 这里需要一个名叫isRouteDisplayed()的方法,这个方法是必须的。用来获取地图API密匙,一旦获得就会传给上边提到的apiKey这个变量。 */@Overrideprotected boolean isRouteDisplayed() {return false;}}

main.xml

<?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" >    <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" > <com.google.android.maps.MapView    android:id="@+id/mapView"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:enabled="true"    android:clickable="true"    android:apiKey="0S3Q4ZaVtVDhibBF6eIGl30D9JDmXVdvP4sQvnQ"    />      </LinearLayout>   </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.easyway.android.google" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">  <uses-library android:name="com.google.android.maps" />  <activity android:name=".AndroidGoogleMapActivity"  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>  <!--   --> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
 

更多相关文章

  1. android如何往SDCard中存取图片
  2. Android(安卓)获取AndroidManifest.xml 中 meta-data 的值
  3. 获取Android系统程序信息
  4. Android如何从服务器获取图片
  5. [Android]获取网络连接状态
  6. Android(安卓)获取设备宽高分辨率
  7. Android中获取和设置手机的壁纸
  8. Android获取手机的型号和系统版本
  9. Android获取sdcard信息

随机推荐

  1. DB为何大量出现select @@session.tx_read
  2. MySQL5.7.20解压版安装和修改root密码的
  3. CentOS7.4 源码安装MySQL8.0的教程详解
  4. centos7上mysql8.0rpm方式安装教程图解
  5. Linux下如何实现Mysql定时任务
  6. 详解Mysql中日期比较大小的方法
  7. mysql数据库如何实现亿级数据快速清理
  8. MySQL存储过程的权限问题小结
  9. mysql zip 文件安装教程
  10. 将phpstudy中的mysql迁移至Linux教程