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)
下面是解决这两种解析的工具类:
- package org.wwj.map;
- import java.io.InputStream;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONObject;
- public class ConvertUtil
- {
- // 根据地址获取对应的经纬度
- public static double[] getLocationInfo(String address)
- {
- // 定义一个HttpClient,用于向指定地址发送请求
- HttpClient client = new DefaultHttpClient();
- // 向指定地址发送GET请求
- HttpGet httpGet = new HttpGet("http://maps.google."
- + "com/maps/api/geocode/json?address=" + address
- + "ka&sensor=false");
- StringBuilder sb = new StringBuilder();
- try
- {
- // 获取服务器的响应
- HttpResponse response = client.execute(httpGet);
- HttpEntity entity = response.getEntity();
- // 获取服务器响应的输入流
- InputStream stream = entity.getContent();
- int b;
- // 循环读取服务器响应
- while ((b = stream.read()) != -1)
- {
- sb.append((char) b);
- }
- // 将服务器返回的字符串转换为JSONObject对象
- JSONObject jsonObject = new JSONObject(sb.toString());
- // 从JSONObject对象中取出代表位置的location属性
- JSONObject location = jsonObject.getJSONArray("results")
- .getJSONObject(0)
- .getJSONObject("geometry").getJSONObject("location");
- // 获取经度信息
- double longitude = location.getDouble("lng");
- // 获取纬度信息
- double latitude = location.getDouble("lat");
- // 将经度、纬度信息组成double[]数组
- return new double[]{longitude , latitude};
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
- // 根据经纬度获取对应的地址
- public static String getAddress(double longitude
- , double latitude)
- {
- // 定义一个HttpClient,用于向指定地址发送请求
- HttpClient client = new DefaultHttpClient();
- // 向指定地址发送GET请求
- HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/"
- + "geocode/json?latlng="
- + latitude + "," + longitude
- + "&sensor=false®ion=cn");
- StringBuilder sb = new StringBuilder();
- try
- {
- // 执行请求
- HttpResponse response = client.execute(httpGet);
- HttpEntity entity = response.getEntity();
- // 获取服务器响应的字符串
- InputStream stream = entity.getContent();
- int b;
- while ((b = stream.read()) != -1)
- {
- sb.append((char) b);
- }
- // 把服务器相应的字符串转换为JSONObject
- JSONObject jsonObj = new JSONObject(sb.toString());
- // 解析出响应结果中的地址数据
- return jsonObj.getJSONArray("results").getJSONObject(0)
- .getString("formatted_address");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
- }
创建项目:AddressLocMap
项目运行效果:
代码清单:
AddressLocMap.java
PosOverLay.java
- package org.wwj.map;
- import java.util.List;
- 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.Toast;
- 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 AddressLocMap extends MapActivity {
- //定义界面上的可视化控件
- Button locBn;
- MapView mv;
- EditText etAddress;
- //定义MapController对象
- MapController controller;
- Bitmap posBitmap;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);
- //获取界面上MapView对象
- mv = (MapView) findViewById(R.id.mv);
- etAddress = (EditText) findViewById(R.id.address);
- //设置放大、缩小控制的按钮
- mv.setBuiltInZoomControls(true);
- //创建MapController对象
- controller = mv.getController();
- //获得Button对象
- locBn = (Button) findViewById(R.id.loc);
- locBn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String address = etAddress.getEditableText().toString().trim();
- if(address.equals("")){
- //判断是否输入空值
- Toast.makeText(AddressLocMap.this, "请输入有效的地址!", Toast.LENGTH_LONG).show();
- return;
- }
- //调用ConverUtil执行地址解析
- double[] results = ConvertUtil.getLocationInfo(address);
- //调用方法更新MapView
- updateMapView(results[0], results[1]);
- }
- });
- //触发按钮的单击事件
- locBn.performClick();
- }
- @Override
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return true;
- }
- //根据经度、纬度将MapView定位到指定地点的方法
- private void updateMapView(double lng, double lat){
- GeoPoint gp = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
- //设置显示放大缩小按钮
- mv.displayZoomControls(true);
- //将地图移动到指定的地理位置
- controller.animateTo(gp);
- //获得MapView上原有的Overlay对象
- List<Overlay> ol = mv.getOverlays();
- //清除原有的Overlay对象
- ol.clear();
- //添加一个新的overLay对象
- ol.add(new PosOverLay(gp, posBitmap));
- }
- }
- package org.wwj.map;
- 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{
- //定义该PosOverLay所绘制的位图
- Bitmap posBitmap;
- //定义该PosOverLay绘制位图的位置
- 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) {
- // TODO Auto-generated method stub
- if(!shadow){
- //获取MapView的Projection对象
- Projection proj = mapView.getProjection();
- Point p = new Point();
- //将真实的地理坐标转换为屏幕上的坐标
- proj.toPixels(gp, p);
- //在指定位置绘制图片
- canvas.drawBitmap(posBitmap, p.x - posBitmap.getWidth() / 2
- , p.y - posBitmap.getHeight() / 2, null);
- }
- }
- }
转载:http://blog.csdn.net/wwj_748/article/details/7840128