Android development: Baidu map transfer a detailed address to get the location on the map

Source: Internet
Author: User

You must click an address to directly call Baidu map and display the address on Baidu map. The parameter has only one address and no corresponding coordinates, this reminds me that the previous map was used to transmit a coordinate and display the address on the map. The idea was as follows: how to convert a detailed address to a coordinate, and then it is sent to Baidu map, but it seems that although the detailed address can be converted into coordinates, but this is converted after the display address, so there is a null pointer, and I have not studied it again, I think another way of thinking, I saw an input address on the Baidu map api on the Internet and click search to display the address on the map. I think this should be fine, so let's take a look at the java file GeoCoder in Baidu demo. Enter an address and click the search button to display the address on the map, click "Search" to call the geocode ("detailed address", "Specific City") of MKSearch. Then, I called this method after initializing the map on the page where I called the map, after clicking the button, it will be loaded all the time and the location will not be displayed. The only difference between me and him is Baidu map demo triggers this event by clicking, and I call this method after initializing the map. But why? Why is it always loaded but not displayed.

The following code is the Baidu map demo code.

// Initialize the search module and register the event listening mSearch = new MKSearch (); mSearch. init (app. mBMapMan, 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 (GeoCoder. this, str, Toast. LENGTH_LONG ). show (); return;} mMapView. getController (). animateTo (res. geoPt); String strInfo = String. format ("latitude: % f longitude: % f \ r \ n", res. geoPt. getLatitudeE6 ()/1e6, res. geoPt. getLongitudeE6 ()/1e6); Toast. makeText (GeoCoder. this, strInfo, Toast. LENGTH_LONG ). show (); Drawable marker = getResources (). getDrawable (R. drawable. ic Onmarka); // obtain the resource marker to be marked on the map. setBounds (0, 0, marker. getIntrinsicWidth (), marker. getIntrinsicHeight (); // defines the location and boundary mMapView for the maker. getOverlays (). clear (); mMapView. getOverlays (). add (new OverItemT (marker, GeoCoder. this, res. geoPt, res. strAddr);} public void onGetPoiResult (MKPoiResult res, int type, int error) {if (error! = 0 | res = null) {Toast. makeText (GeoCoder. this, "resolution failed", Toast. LENGTH_LONG). show (); return;} if (res! = Null & res. getCurrentNumPois ()> 0) {GeoPoint ptGeo = res. getAllPoi (). get (0 ). pt; // move the map to this point: mMapView. getController (). animateTo (ptGeo); String strInfo = String. format ("latitude: % f longitude: % f \ r \ n", ptGeo. getLatitudeE6 ()/1e6, ptGeo. getLongitudeE6 ()/1e6); strInfo + = "\ r \ n:"; for (int I = 0; I <res. getAllPoi (). size (); I ++) {strInfo + = (res. getAllPoi (). get (I ). name + ";");} Toast. makeText (GeoCoder. this, strInfo, Toast. LENGTH_LONG ). show () ;}} public void initialize (ores res, int error) {} public void onGetTransitRouteResult (MKTransitRouteResult res, int error) {} public void onGetWalkingRouteResult (MKWalkingRouteResult, int error) {} public void onGetBusDetailResult (MKBusLineResult result, int iError) {}@ Overridepublic void onGetSuggestionResult (MKSuggestionResult res, int arg1) {// TODO Auto-generated method stub} @ Override public void ongetrgc1_urlresult (String arg0, int arg1) {// TODO Auto-generated method stub }});

EditText editCity = (EditText)findViewById(R.id.city);EditText editGeoCodeKey = (EditText)findViewById(R.id.geocodekey);mSearch.geocode(editGeoCodeKey.getText().toString(), editCity.getText().toString());



Later, some netizens said that this method was a passive method. To actively call it, we recommend that you use a thread to call it. After initializing the map, I wrote a thread, the geocode ("detailed address", "Specific City") method of MKSearch is called in the thread, and the loading is successful. However, one problem is that I have to click the screen to refresh the map and start searching on the Internet, that is, to refresh the androidmanifest IN THE AndroidMainfest. xml file. Android: targetSdkVersion = "10" to remove it. It is successful!

But there is another problem. After the address is displayed on the map, I want to bring up a detailed address and keep it on the map, now you can place the landmark on the map. The following code writes it to the MKSearchListener () Listener event ()

Drawable marker = getResources (). getDrawable (R. drawable. point_start); // obtain the resource marker to be marked on the map. setBounds (0, 0, marker. getIntrinsicWidth (), marker. getIntrinsicHeight (); // defines the location and boundary View mPopView = getLayoutInflater () for the maker (). inflate (R. layout. popview2, null); OverItemT overitemr = new overitemr (marker, LocationActivity. this, res. geoPt, res. strAddr );


But I want to bring up a bubble with a detailed address. What should I do? In Baidu map demo, the Java class ItemizedOverlayDemo can display bubbles, however, after clicking the landmark, a bubble will pop up. The following code is as follows:

// Process when the click event protected boolean onTap (int I) {setFocus (mGeoList. get (I); // update the bubble position and display GeoPoint pt = mGeoList. get (I ). getPoint (); ItemizedOverlayDemo. mMapView. updateViewLayout (ItemizedOverlayDemo. mPopView, new MapView. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, pt, MapView. layoutParams. BOTTOM_CENTER); ItemizedOverlayDemo. mPopView. setVisibility (View. VISIBLE); Toast. makeText (this. mContext, mGeoList. get (I ). getSnippet (), Toast. LENGTH_SHORT ). show (); return true ;}


Haha, I wrote the modified Code

I wrote the above Code to the MKSearchListener () Listener event, but an error will be reported at this time, because I did not add the pop-up bubble to the map and then update it. It should be in MKSearchListener () add the following code to the listener

// Create the pop-up bubble mPopView = super when you click mark. getLayoutInflater (). inflate (R. layout. popview, null); mMapView. addView (mPopView, new MapView. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, null, MapView. layoutParams. TOP_LEFT); mPopView. setVisibility (View. GONE );


After the code is added, the detailed address bubble can be displayed at the location of the landmark image. However, the pop-up bubble overlaps with the Landmark image, at the beginning, I wanted to change the position of the bubble in update, but this is not simple.

Point, so later I thought about modifying the bubble's layout file. Haha, I finally succeeded.

Code: http://download.csdn.net/detail/ladyweiwei1234/7129687

See Baidu map demo (1.3.5 version): http://download.csdn.net/detail/ladyweiwei1234/7129701


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.