Android使用Google Map服务-根据地址定位

来源:互联网 发布:如何求矩阵的伴随矩阵 编辑:程序博客网 时间:2024/05/16 16:05

Android使用Google Map服务-根据地址定位

根据地址定位是对根据GPS信息定位的一个改进,我们可以直接输入地址,获取到自己想要定位的地方,这更符合用户的需求。因为Google Map的地图定位是根据经纬度来完成的,所以我们需要根据地址信息,把地址解析成经纬度。这里需要:

1.地址解析 (服务地址为:http://maps.google.com/api/geocode/json?parameters)

2.反向地址解析 (服务地址为:http://maps.google.com/api/geocode/json?lalng=40.714,-73.96&sensor=true_or_false)

下面是解决这两种解析的工具类:

[java] view plaincopyprint?
  1. package org.wwj.map;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import org.apache.http.HttpEntity;  
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpGet;  
  9. import org.apache.http.impl.client.DefaultHttpClient;  
  10. import org.json.JSONObject;  
  11.   
  12. public class ConvertUtil  
  13. {  
  14.     // 根据地址获取对应的经纬度  
  15.     public static double[] getLocationInfo(String address)  
  16.     {  
  17.         // 定义一个HttpClient,用于向指定地址发送请求  
  18.         HttpClient client = new DefaultHttpClient();  
  19.         // 向指定地址发送GET请求  
  20.         HttpGet httpGet = new HttpGet("http://maps.google."  
  21.             + "com/maps/api/geocode/json?address=" + address  
  22.             + "ka&sensor=false");  
  23.         StringBuilder sb = new StringBuilder();  
  24.         try  
  25.         {  
  26.             // 获取服务器的响应  
  27.             HttpResponse response = client.execute(httpGet);  
  28.             HttpEntity entity = response.getEntity();  
  29.             // 获取服务器响应的输入流  
  30.             InputStream stream = entity.getContent();  
  31.             int b;  
  32.             // 循环读取服务器响应  
  33.             while ((b = stream.read()) != -1)  
  34.             {  
  35.                 sb.append((char) b);  
  36.             }  
  37.             // 将服务器返回的字符串转换为JSONObject对象  
  38.             JSONObject jsonObject = new JSONObject(sb.toString());  
  39.             // 从JSONObject对象中取出代表位置的location属性  
  40.             JSONObject location = jsonObject.getJSONArray("results")  
  41.                 .getJSONObject(0)     
  42.                 .getJSONObject("geometry").getJSONObject("location");  
  43.             // 获取经度信息  
  44.             double longitude = location.getDouble("lng");  
  45.             // 获取纬度信息  
  46.             double latitude = location.getDouble("lat");  
  47.             // 将经度、纬度信息组成double[]数组  
  48.             return new double[]{longitude , latitude};            
  49.         }  
  50.         catch (Exception e)  
  51.         {  
  52.             e.printStackTrace();  
  53.         }  
  54.         return null;  
  55.     }   
  56.     // 根据经纬度获取对应的地址  
  57.     public static String getAddress(double longitude  
  58.         , double latitude)  
  59.     {  
  60.         // 定义一个HttpClient,用于向指定地址发送请求  
  61.         HttpClient client = new DefaultHttpClient();  
  62.         // 向指定地址发送GET请求  
  63.         HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/"  
  64.             + "geocode/json?latlng="  
  65.             + latitude + "," + longitude   
  66.             + "&sensor=false®ion=cn");  
  67.         StringBuilder sb = new StringBuilder();  
  68.         try  
  69.         {  
  70.             // 执行请求  
  71.             HttpResponse response = client.execute(httpGet);  
  72.             HttpEntity entity = response.getEntity();  
  73.             // 获取服务器响应的字符串  
  74.             InputStream stream = entity.getContent();  
  75.             int b;  
  76.             while ((b = stream.read()) != -1)  
  77.             {  
  78.                 sb.append((char) b);  
  79.             }  
  80.             // 把服务器相应的字符串转换为JSONObject  
  81.             JSONObject jsonObj = new JSONObject(sb.toString());  
  82.             // 解析出响应结果中的地址数据  
  83.             return jsonObj.getJSONArray("results").getJSONObject(0)  
  84.                 .getString("formatted_address");  
  85.         }  
  86.         catch (Exception e)  
  87.         {  
  88.             e.printStackTrace();  
  89.         }  
  90.         return null;  
  91.     }     
  92. }  


创建项目:AddressLocMap

项目运行效果:

 

代码清单:

AddressLocMap.java

PosOverLay.java

[java] view plaincopyprint?
  1. package org.wwj.map;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. import com.google.android.maps.GeoPoint;  
  15. import com.google.android.maps.MapActivity;  
  16. import com.google.android.maps.MapController;  
  17. import com.google.android.maps.MapView;  
  18. import com.google.android.maps.Overlay;  
  19.   
  20. public class AddressLocMap extends MapActivity {  
  21.       
  22.     //定义界面上的可视化控件  
  23.     Button locBn;  
  24.     MapView mv;  
  25.     EditText etAddress;  
  26.     //定义MapController对象  
  27.     MapController controller;  
  28.     Bitmap posBitmap;  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);  
  34.         //获取界面上MapView对象  
  35.         mv = (MapView) findViewById(R.id.mv);  
  36.         etAddress = (EditText) findViewById(R.id.address);  
  37.         //设置放大、缩小控制的按钮  
  38.         mv.setBuiltInZoomControls(true);  
  39.         //创建MapController对象  
  40.         controller = mv.getController();  
  41.         //获得Button对象  
  42.         locBn = (Button) findViewById(R.id.loc);  
  43.         locBn.setOnClickListener(new OnClickListener() {  
  44.               
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub  
  47.                 String address = etAddress.getEditableText().toString().trim();  
  48.                 if(address.equals("")){  
  49.                     //判断是否输入空值  
  50.                     Toast.makeText(AddressLocMap.this"请输入有效的地址!", Toast.LENGTH_LONG).show();  
  51.                     return;  
  52.                 }  
  53.                 //调用ConverUtil执行地址解析  
  54.                 double[] results = ConvertUtil.getLocationInfo(address);  
  55.                 //调用方法更新MapView  
  56.                 updateMapView(results[0], results[1]);  
  57.             }  
  58.         });  
  59.         //触发按钮的单击事件  
  60.         locBn.performClick();  
  61.     }  
  62.     @Override  
  63.     protected boolean isRouteDisplayed() {  
  64.         // TODO Auto-generated method stub  
  65.         return true;  
  66.     }  
  67.     //根据经度、纬度将MapView定位到指定地点的方法  
  68.     private void updateMapView(double lng, double lat){  
  69.         GeoPoint gp = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));  
  70.         //设置显示放大缩小按钮  
  71.         mv.displayZoomControls(true);  
  72.         //将地图移动到指定的地理位置  
  73.         controller.animateTo(gp);  
  74.         //获得MapView上原有的Overlay对象  
  75.         List<Overlay> ol = mv.getOverlays();  
  76.         //清除原有的Overlay对象  
  77.         ol.clear();  
  78.         //添加一个新的overLay对象  
  79.         ol.add(new PosOverLay(gp, posBitmap));  
  80.     }  
  81.       
  82. }  


 

[java] view plaincopyprint?
  1. package org.wwj.map;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Point;  
  6.   
  7. import com.google.android.maps.GeoPoint;  
  8. import com.google.android.maps.MapView;  
  9. import com.google.android.maps.Overlay;  
  10. import com.google.android.maps.Projection;  
  11.   
  12. public class PosOverLay extends Overlay{  
  13.       
  14.     //定义该PosOverLay所绘制的位图  
  15.     Bitmap posBitmap;  
  16.     //定义该PosOverLay绘制位图的位置  
  17.     GeoPoint gp;  
  18.     public PosOverLay(GeoPoint gp, Bitmap posBitmap){  
  19.         super();  
  20.         this.gp = gp;  
  21.         this.posBitmap = posBitmap;  
  22.     }  
  23.     @Override  
  24.     public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
  25.         // TODO Auto-generated method stub  
  26.         if(!shadow){  
  27.             //获取MapView的Projection对象  
  28.             Projection proj = mapView.getProjection();  
  29.             Point p = new Point();  
  30.             //将真实的地理坐标转换为屏幕上的坐标  
  31.             proj.toPixels(gp, p);  
  32.             //在指定位置绘制图片  
  33.             canvas.drawBitmap(posBitmap, p.x - posBitmap.getWidth() / 2  
  34.                     , p.y - posBitmap.getHeight() / 2null);  
  35.         }  
  36.           
  37.     }  
  38.       
  39.   
转载:http://blog.csdn.net/wwj_748/article/details/7840128
原创粉丝点击