标签:
搜索地址功能是建立在能够通过百度地图API获取位置的基础上
通过百度地图定位获取位置详情:http://www.cnblogs.com/zzw1994/p/5008134.html
1 package com.zzw.baidumappoint; 2 3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7 import com.baidu.mapapi.SDKInitializer; 8 import com.baidu.mapapi.map.BaiduMap; 9 import com.baidu.mapapi.map.MapStatusUpdate; 10 import com.baidu.mapapi.map.MapStatusUpdateFactory; 11 import com.baidu.mapapi.map.MapView; 12 import com.baidu.mapapi.map.MyLocationData; 13 import com.baidu.mapapi.model.LatLng; 14 import com.baidu.mapapi.search.geocode.GeoCodeOption; 15 import com.baidu.mapapi.search.geocode.GeoCodeResult; 16 import com.baidu.mapapi.search.geocode.GeoCoder; 17 import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; 18 import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; 19 20 import android.app.Activity; 21 import android.os.Bundle; 22 import android.view.View; 23 import android.view.View.OnClickListener; 24 import android.widget.EditText; 25 import android.widget.Toast; 26 27 public class MainActivity extends Activity { 28 29 private LocationClient mLocationClient; 30 private BaiduMap mBaiduMap; 31 private GeoCoder mGeoCoder; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 SDKInitializer.initialize(getApplicationContext());// 使程序不崩溃的作用 37 setContentView(R.layout.activity_main); 38 39 final EditText city_edit = (EditText) findViewById(R.id.city); 40 final EditText addr_edit = (EditText) findViewById(R.id.addr); 41 // 获取LocationClient 42 mLocationClient = new LocationClient(this); 43 44 // 获得GeoCoder 45 mGeoCoder = GeoCoder.newInstance(); 46 mGeoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { 47 48 @Override 49 public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { 50 51 } 52 53 @Override 54 public void onGetGeoCodeResult(GeoCodeResult result) { 55 try{ 56 LatLng ll = result.getLocation(); 57 // 只是完成了定位 58 MyLocationData locData = new MyLocationData.Builder().latitude(ll.latitude).longitude(ll.longitude) 59 .build(); 60 61 // 设置图标在地图上的位置 62 mBaiduMap.setMyLocationData(locData); 63 64 // 开始移动百度地图的定位地点到中心位置 65 MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16.0f); 66 mBaiduMap.animateMapStatus(u); 67 }catch(Exception e){ 68 Toast.makeText(getApplicationContext(), "请检查地址", 0).show(); 69 } 70 } 71 }); 72 73 findViewById(R.id.GO).setOnClickListener(new OnClickListener() { 74 75 @Override 76 public void onClick(View v) { 77 try { 78 String city = city_edit.getText() + ""; 79 String addr = addr_edit.getText() + ""; 80 city_edit.setText(""); 81 addr_edit.setText(""); 82 mGeoCoder.geocode(new GeoCodeOption().city(city.trim()).address(addr.trim())); 83 } catch (Exception e) { 84 85 } 86 87 } 88 }); 89 LocationClientOption option = new LocationClientOption(); 90 option.setCoorType("bd09ll"); 91 mLocationClient.setLocOption(option); 92 93 // 获取BaiduMap 94 MapView mapView = (MapView) findViewById(R.id.mapView); 95 mBaiduMap = mapView.getMap(); 96 97 // 显示出当前位置的小图标 98 mBaiduMap.setMyLocationEnabled(true); 99 100 MyLocationListener mListener = new MyLocationListener(); 101 mLocationClient.registerLocationListener(mListener); 102 mLocationClient.start(); 103 } 104 105 @Override 106 protected void onDestroy() { 107 mLocationClient.stop(); 108 super.onDestroy(); 109 } 110 111 private class MyLocationListener implements BDLocationListener { 112 113 @Override 114 public void onReceiveLocation(BDLocation location) { 115 116 // 只是完成了定位 117 MyLocationData locData = new MyLocationData.Builder().latitude(location.getLatitude()) 118 .longitude(location.getLongitude()).build(); 119 120 // 设置图标在地图上的位置 121 mBaiduMap.setMyLocationData(locData); 122 123 // 开始移动百度地图的定位地点到中心位置 124 LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); 125 MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16.0f); 126 mBaiduMap.animateMapStatus(u); 127 } 128 129 } 130 131 }
通过百度地图API实现搜索地址--第三方开源--百度地图(三)
标签:
原文地址:http://www.cnblogs.com/zzw1994/p/5009656.html