Conversion of address information and coordinates of Baidu Map

Source: Internet
Author: User

In practical use, it is often necessary to carry out Geographic Encoding and geographic anti-encoding, that is, converting the address information into coordinates and converting the coordinates into address information, this demo is used to demonstrate how to perform Geographic Encoding search (using address to retrieve coordinates), anti-Geographic Encoding search (using coordinates to retrieve addresses), and how to use ItemizedOverlay to mark result points on a map, the Code prototype is from Baidu Demo. The Code is as follows: Activity:

Package com. home; import android. app. activity; import android. graphics. drawable. drawable; 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. baidu. mapapi. map. itemizedOverlay; import com. baidu. mapapi. map. mapView; import com. baidu. mapapi. map. overlayItem; imp Ort 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. mkw.urlresult; import com. baidu. mapapi. search. MKSuggestionResult; import com. baidu. mapapi. s Earch. MKTransitRouteResult; import com. baidu. mapapi. search. MKWalkingRouteResult; import com. baidu. platform. comapi. basestruct. geoPoint; public class GeoCoderActivity extends Activity implements OnClickListener {// private Button related to UI mBtnReverseGeoCode = null; // decomcodes coordinates to private Button mBtnGeoCode = null; // encode the address as coordinate private EditText lat = null; private EditText lon = null; private EditText edit City = null; private EditText editGeoCodeKey = null; // map-related private MapView mMapView = null; // map View // search-related private MKSearch mSearch = null; // search module, you can also remove the map module from using protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); DemoApplication app = (DemoApplication) this. getApplication (); setContentView (R. layout. geocoder); CharSequence titleLable = "geocoding function"; setTitle (ti TleLable); // map initialization mMapView = (MapView) findViewById (R. id. geocoder_bmapView); mMapView. getController (). enableClick (true); mMapView. getController (). setZoom (12); // UI initialization lat = (EditText) findViewById (R. id. geocoder_et_lat); lon = (EditText) findViewById (R. id. geocoder_et_lon); editCity = (EditText) findViewById (R. id. geocoder_et_city); editGeoCodeKey = (EditText) findViewById (R. id. geocoder_et_geocode Key); mBtnReverseGeoCode = (Button) findViewById (R. id. geocoder_btn_reversegeocode); mBtnGeoCode = (Button) findViewById (R. id. geocoder_btn_geocode); mBtnReverseGeoCode. setOnClickListener (this); mBtnGeoCode. setOnClickListener (this); // initialize the search module and register the event listener mSearch = new MKSearch (); mSearch. init (app. mBMapManager, new MKSearchListener () {@ Override public void onGetPoiDetailSearchResult (int type, int error) {} Public void onGetAddrResult (MKAddrInfo res, int error) {if (error! = 0) {String str = String. format ("error code: % d", error); Toast. makeText (GeoCoderActivity. this, str, Toast. LENGTH_LONG ). show (); return;} // map to this point mMapView. getController (). animateTo (res. geoPt); if (res. type = MKAddrInfo. MK_GEOCODE) {// geographic code: String strInfo = String. format ("latitude: % f longitude: % f", res. geoPt. getLatitudeE6 ()/1e6, res. geoPt. getLongitudeE6 ()/1e6); Toast. makeText (GeoCoderActivity. this, strInfo, Toast. LENGTH_LONG ). show ();} if (res. type = MKAddrInfo. MK_REVERSEGEOCODE) {// anti-geographic code: retrieves the detailed address and the surrounding poi String strInfo = res Through coordinate points. strAddr; Toast. makeText (GeoCoderActivity. this, strInfo, Toast. LENGTH_LONG ). show () ;}// generate the ItemizedOverlay layer to mark the result point ItemizedOverlay <OverlayItem> itemOverlay = new ItemizedOverlay <OverlayItem> (null, mMapView ); // generate Item OverlayItem item = new OverlayItem (res. geoPt, "", null); // obtain the resource Drawable marker = getResources () to be marked on the map (). getDrawable (R. drawable. icon_markf); // defines the location and boundary for the maker. setBounds (0, 0, marker. getIntrinsicWidth (), marker. getIntrinsicHeight (); // set marker item for item. setMarker (marker); // Add item itemOverlay to the layer. addItem (item); // clear other map layer mMapView. getOverlays (). clear (); // Add an annotation ItemizedOverlay layer mMapView. getOverlays (). add (itemOverlay); // execute refresh to make the mMapView take effect. refresh ();} public void onGetPoiResult (MKPoiResult res, int type, int error) {} public void evaluate (destination res, int error) {} public void onGetTransitRouteResult (MKTransitRouteResult res, int error) {} public void Merge (mkreceivingrouteresult res, int error) {} public void onGetBusDetailResult (MKBusLineResult result, int iError) {} @ Override public void Merge (MKSuggestionResult res, int arg1) {}@ Override public void onget1_urlresult (mk1_urlresult result, int type, int error) {}}) ;}@ Override public void onClick (View v) {if (v = mBtnGeoCode) {// Geo search mSearch. geocode (editGeoCodeKey. getText (). toString (), editCity. getText (). toString ();} if (v = mBtnReverseGeoCode) {GeoPoint ptCenter = new GeoPoint (int) (Float. valueOf (lat. getText (). toString () * 1e6), (int) (Float. valueOf (lon. getText (). toString () * 1e6); // search for mSearch in anti-Geo mode. reverseGeocode (ptCenter) ;}@ Override protected void onPause () {mMapView. onPause (); super. onPause () ;}@ Override protected void onResume () {mMapView. onResume (); super. onResume () ;}@ Override protected void onDestroy () {mMapView. destroy (); super. onDestroy () ;}@ Override protected void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); mMapView. onSaveInstanceState (outState) ;}@ Override protected void onRestoreInstanceState (Bundle savedInstanceState) {super. onRestoreInstanceState (savedInstanceState); mMapView. onRestoreInstanceState (savedInstanceState );}}

 

Layout XML:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <EditText android: id = "@ + id/geocoder_et_city" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Beijing"/> <EditText android: id = "@ + id/geocoder_et_geocodekey" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "10 Shangdi 10 Street, Haidian District"/> <Button android: id = "@ + id/geocoder_btn_geocode" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ drawable/button_style" android: text = "Geo"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <EditText android: id = "@ + id/geocoder_et_lat" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "39.904965"/> <EditText android: id = "@ + id/geocoder_et_lon" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "116.327764"/> <Button android: id = "@ + id/geocoder_btn_reversegeocode" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ drawable/button_style" android: text = "ReverseGeo"/> </LinearLayout> <com. baidu. mapapi. map. mapView android: id = "@ + id/geocoder_bmapView" android: layout_width = "match_parent" android: layout_height = "match_parent" android: clickable = "true"/> </LinearLayout>

 

The effect of the configuration file is the same as that of the previous map example attachment:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.