快捷搜索:  xxx  as  1111

Android百度地图之实现根据城市名,地址名获取

场景:我知道某个城市名和这个城市里的一个小地方的地址,我想知道哪这个城市里这个小地方的GPS纬度、经度值。

例如:我想知道上海市,益江路盛夏路的GPS纬度、经度值是若干?

注:一些基础的观点什么的就都不讲了,前面已经讲过了,想要懂得的请查看:Android百度舆图SDK版本v2.0.0试用

一、假设基于百度舆图SDK,底图(最基础的图层)你已经实现了,并且设置显示的舆图为上海市的。

二、获取上海市,益江路盛夏路的GPS纬度、经度值:

1、创建MKSearch类的实例,并初始化。

mMKSearch = new MKSearch();

mMKSearch.init(mMapManager, new MKSearchListener(){

@Overridepublic void onGetAddrResult(MKAddrInfo result, int error) {

}

@Override

public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {// TODO Auto-generated method stub

}

@Override

public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {

}

@Overridepublic void onGetPoiDetailSearchResult(int arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {

// TODO Auto-generated method stub

}

});

2、调用mMKSearch.geocode(Stringaddress,String city)措施,返回值是int类型的。

参数列表:某个城市中某个地方的地址,城市名。例如:陆家嘴,上海市

mMKSearch.geocode("益江路盛夏路", "上海市");

3、在回调onGetAddrResult(MKAddrInfo result, int error)里面处置惩罚查到的结果。

将获得的结果用两种要领出现:a. 以对话框的形式展现;b. 在舆图上标注出来。

@Override

public void onGetAddrResult(MKAddrInfo result, int error) {// error的含义: 差错号,0表示结果精确,result中有相关结果信息;

// 100表示结果精确,result中无相关地址信息if(error != 0 || result == null){

String str = String.format("差错号:%d", error);Toast.makeText(BaiduMapGeocodeActivity.this, str, Toast.LENGTH_LONG).show();

return;}

GeoPoint mPoint = result.geoPt;

// 将获得的结果以对话框的形式展现

showGeocode(result.strAddr, mPoint);

// 将找到的位置设置为在手机屏幕内可见解图的中间点mMapView.getController().animateTo(mPoint);

// 获得必要标在舆图上的资本

Drawable marker = getResources().getDrawable(R.drawable.icon_marka);// 为maker定义位置和界限

// marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

mMapView.getOverlays().clear();mMapView.getOverlays().add(new MyOverlay(marker, mPoint, result.strAddr));

mMapView.refresh();}

运行效果图如下:

a. 对话框形式

b. 在舆图上标注

完备代码:

package com.android.baidu.map;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;import android.content.Context;

import android.content.res.Configuration;import android.graphics.drawable.Drawable;

import android.os.Bundle;import android.util.Log;

import android.widget.Toast;

import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.MKGeneralListener;

import com.baidu.mapapi.map.ItemizedOverlay;import com.baidu.mapapi.map.MKEvent;

import com.baidu.mapapi.map.MapController;import com.baidu.mapapi.map.MapView;

import com.baidu.mapapi.map.OverlayItem;import com.baidu.mapapi.search.MKAddrInfo;

import com.baidu.mapapi.search.MKBusLineResult;import com.baidu.mapapi.search.MKDrivingRouteResult;

import com.baidu.mapapi.search.MKPoiResult;import com.baidu.mapapi.search.MKSearch;

import com.baidu.mapapi.search.MKSearchListener;import com.baidu.mapapi.search.MKSuggestionResult;

import com.baidu.mapapi.search.MKTransitRouteResult;import com.baidu.mapapi.search.MKWalkingRouteResult;

import com.baidu.platform.comapi.basestruct.GeoPoint;

/奸淫 根据城市名,地址名获取地址信息 (GPS纬度、经度值)

* @author android_ls*

*/ public class BaiduMapGeocodeActivity extends Activity {

public static final String TAG = "BaiduMapGeocodeActivity";

public static final String BAIDU_MAP_KEY = "07418AEC69BAAB7104C6230A6120C580DFFAEEBB";

private BMapManager mMapManager = null;

private MapView mMapView = null;

private MKSearch mMKSearch = null;

private Context mContext;

@Override

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

mContext = BaiduMapGeocodeActivity.this.getApplicationContext();

mMapManager = new BMapManager(getApplicationContext());

mMapManager.init(BAIDU_MAP_KEY, new MKGeneralListener() {

@Overridepublic void onGetNetworkState(int errorCode) {

if (errorCode == MKEvent.ERROR_NETWORK_CONNECT) {Toast.makeText(mContext, "您的收集掉足啦!",

Toast.LENGTH_LONG).show();}

}

@Overridepublic void onGetPermissionState(int errorCode) {

if (errorCode == MKEvent.ERROR_PERMISSION_DENIED) {// 授权Key差错:

Toast.makeText(mContext,"请在 DemoApplication.java文件输入精确的授权Key!",

Toast.LENGTH_LONG).show();}

}});

setContentView(R.layout.main);

mMapView = (MapView) this.findViewById(R.id.bmapsView);

mMapView.setBuiltInZoomControls(true);

MapController mMapController = mMapView.getController();// 上海市的GPS纬度经度值:31.232310,121.469160

GeoPoint mGeoPoint = new GeoPoint((int) (31.232310 * 1E6),

(int) (121.469160 * 1E6));

mMapController.setCenter(mGeoPoint);

mMapController.setZoom(18);

mMKSearch = new MKSearch();mMKSearch.init(mMapManager, new MKSearchListener(){

@Override

public void onGetAddrResult(MKAddrInfo result, int error) {// error的含义: 差错号,0表示结果精确,result中有相关结果信息;

// 100表示结果精确,result中无相关地址信息if(error != 0 || result == null){

String str = String.format("差错号:%d", error);Toast.makeText(BaiduMapGeocodeActivity.this,

str, Toast.LENGTH_LONG).show();return;

}

GeoPoint mPoint = result.geoPt;

// 将获得的结果以对话框的形式展现showGeocode(result.strAddr, mPoint);

// 将找到的位置设置为在手机屏幕内可见解图的中间点

mMapView.getController().animateTo(mPoint);

// 获得必要标在舆图上的资本Drawable marker = getResources().getDrawable(R.drawable.icon_marka);

// 为maker定义位置和界限// marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

mMapView.getOverlays().clear();

mMapView.getOverlays().add(new MyOverlay(marker, mPoint, result.strAddr));

mMapView.refresh();}

@Override

public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {// TODO Auto-generated method stub

}

@Override

public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {

}

@Overridepublic void onGetPoiDetailSearchResult(int arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {

// TODO Auto-generated method stub

}

@Overridepublic void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {

// TODO Auto-generated method stub

}

});

// 根据地址名获取地址信息 异步函数,返回结果在MKSearchListener里的onGetAddrResult措施看护mMKSearch.geocode("益江路盛夏路", "上海市");

// 测试:下面的都获取不到GPS纬度、经度值。感慨:百度舆图这方面有待前进。

// mMKSearch.geocode("浦东软件园", "上海市");// mMKSearch.geocode("广兰路", "上海市");

// mMKSearch.geocode("祖冲之路金科路", "上海市");// mMKSearch.geocode("陆家嘴", "上海市");

}

/**

* 以Dialog的形式显示查找结果* @param address 地址

* @param mPoint 地舆坐标点*/

private void showGeocode(String address, GeoPoint mPoint){

// 将以微度的整数形式存储的地舆坐标点转换成GPS纬度经度值double latitude= mPoint.getLatitudeE6()/1E6; // 纬度

double longitude = mPoint.getLongitudeE6()/1E6;// 经度Log.i(TAG, "latitude = " + latitude + "\t longitude = " + longitude);

StringBuilder sb = new StringBuilder(address);

sb.append("\n").append("纬度:\t").append(latitude)

.append("\n").append("经度:\t").append(longitude);

new AlertDialog.Builder(this)

.setTitle("根据地址名获取GPS纬度、经度值").setMessage(sb)

.show();}

class MyOverlay extends ItemizedOverlay{

private ArrayList overlayList = new ArrayList();

public MyOverlay(Drawable drawable, GeoPoint point, String address) {

super(drawable);overlayList.add(new OverlayItem(point, address, address));

populate();}

@Override

protected OverlayItem createItem(int index) {return overlayList.get(index);

}

@Overridepublic int size() {

return overlayList.size();}

}

@Override

public void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);

}

@Overrideprotected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);mMapView.onSaveInstanceState(outState);

}

@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);mMapView.onRestoreInstanceState(savedInstanceState);

}

@Overrideprotected void onResume() {

mMapView.onResume();

if (mMapManager != null) {mMapManager.start();

}

super.onResume();}

@Override

protected void onPause() {mMapView.onPause();

if (mMapManager != null) {mMapManager.stop();

}super.onPause();

}

@Overrideprotected void onDestroy() {

mMapView.destroy();if (mMapManager != null) {

mMapManager.destroy();mMapManager = null;

}super.onDestroy();

}

}

转自:http://blog.csdn.net/android_ls/article/details/8579695

您可能还会对下面的文章感兴趣: