Simple use of Baidu maps and simple use of Baidu maps

Source: Internet
Author: User

Simple use of Baidu maps and simple use of Baidu maps

I recently wrote a Demo of Baidu map, so I will summarize the problems I encountered;

1. Use Baidu map. Let's take a look at the official example. You need to configure the KEY in the list file. If you want to use the location, you need to register serviece: android: name = "com. baidu. location. f"

2. you need to create an application class and configure it in the configuration file; otherwise, java is always reported. lang. illegalStateException: you have not supplyed the global app context info from SDKInitializer. initialize (Context) function. SDKInitializer. initialize (this;
3. Pay attention to the life cycle of MapView. Remember to create and destroy MapView in time.

4. when locating the current location, obtain the current place name using option. setIsNeedAddress (true); // sets the address information; the original option. setAddrType ("all") expires, but information is still available.

5. When using Marker, marker. getExtraInfo () always reports the null pointer problem. The null is reported because the bundle is not assigned to every Marker when the Marker is added, of course it will get null;

You must add the extraInfo (bundle) field to OverlayOptions.

6.

The following is my demo

Locate yourself first, and then display 6 Marker on the map. When you click each Marker, the current Marker information is displayed.

The main code is as follows:

Package com. it. hello. activity. baidu; import java. util. arrayList; import java. util. list; import com. baidu. location. BDLocation; import com. baidu. location. BDLocationListener; import com. baidu. location. locationClient; import com. baidu. location. locationClientOption; import com. baidu. mapapi. map. baiduMap; import com. baidu. mapapi. map. baiduMap. onMarkerClickListener; import com. baidu. mapapi. map. baiduMap. onMyLocationClickListener; import com. baidu. mapapi. map. bitmapDescriptor; import com. baidu. mapapi. map. bitmapDescriptorFactory; import com. baidu. mapapi. map. infoWindow; import com. baidu. mapapi. map. mapStatus; import com. baidu. mapapi. map. mapStatusUpdateFactory; import com. baidu. mapapi. map. mapView; import com. baidu. mapapi. map. marker; import com. baidu. mapapi. map. markerOptions; import com. baidu. mapapi. map. myLocationData; import com. baidu. mapapi. map. infoWindow. onInfoWindowClickListener; import com. baidu. mapapi. map. markerOptions. markerAnimateType; import com. baidu. mapapi. map. overlayOptions; import com. baidu. mapapi. model. latLng; import com. it. hello. r; import com. it. hello. activity. entity. carInfo; import com. it. hello. activity. util. densityUtils; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. textView;/*** Baidu Marker problem * @ author zh **/public class MyBaidu extends Activity {private MapView mMapView; private BaiduMap mBaiduMap; /** clear Marker */private Button btn_clean;/** information about Marker on the map */private List <CarInfo> carInfos = new ArrayList <CarInfo> (); /** positioning related */private LocationClient mLocClient;/** locate the current location name */private String nowPosition = ""; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_baidu); mMapView = (MapView) findViewById (R. id. baidu_mapView); btn_clean = (Button) findViewById (R. id. baidu_clean); mBaiduMap = mMapView. getMap (); // MapStatusUpdate msu = MapStatusUpdateFactory. zoomTo (12.0f); // mBaiduMap. setMapStatus (msu); // enable the position layer mBaiduMap. setMyLocationEnabled (true); // locate initialization mLocClient = new LocationClient (this); mLocClient. registerLocationListener (new MyLocationListenner (); LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable GPS option. setCoorType ("bd09ll"); // sets the coordinate type option. setScanSpan (1000); // sets the scan interval, in milliseconds. When <1000 (1 s), the scheduled location is invalid. // option. setAddrType ("all"); option. setIsNeedAddress (true); // sets the address information. By default, mLocClient has no address information. setLocOption (option); mLocClient. start ();/*** click the event listening interface on the map positioning icon */mBaiduMap. setOnMyLocationClickListener (new OnMyLocationClickListener () {@ Override public boolean onMyLocationClick () {// TODO Auto-generated method stub // DensityUtils indicates the current location of the toast. showToast (MyBaidu. this, nowPosition); return false ;}}); initOverlay ();/*** Marker click event listening interface */mBaiduMap. setOnMarkerClickListener (new OnMarkerClickListener () {@ Override public boolean onMarkerClick (Marker marker) {// TODO Auto-generated method stub TextView textView = new TextView (getApplicationContext (); textView. setPadding (15, 10, 15, 30); textView. setBackgroundResource (R. drawable. popup); OnInfoWindowClickListener listener = null; System. out. println (marker. getExtraInfo () + "d" + marker); final CarInfo carInfo = (CarInfo) marker. getExtraInfo (). get ("STATION"); textView. setText ("license plate number:" + carInfo. getLicenseNumber (); textView. setTextColor (getResources (). getColor (R. color. action_bar_bg_select); listener = new OnInfoWindowClickListener () {@ Override public void onInfoWindowClick () {// TODO Auto-generated method stub System. out. println (carInfo + carInfo. getLatitude () + "" + carInfo. getlongpolling (); mBaiduMap. hideInfoWindow () ;}}; LatLng ll = marker. getPosition (); InfoWindow mInfoWindow = new InfoWindow (BitmapDescriptorFactory. fromView (textView), ll,-47, listener); mBaiduMap. showInfoWindow (mInfoWindow); return true ;}});/*** clear the Marker and click the event interface */btn_clean.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub mBaiduMap. clear (); carInfos. clear () ;}}) ;}/ *** locate the SDK listener function **/boolean isFirstLoc = true; // whether to locate private class MyLocationListenner implements BDLocationListener {@ Override public void onReceiveLocation (BDLocation location) for the first time) {// TODO Auto-generated method stub if (location = null | mMapView = null) {return;} MyLocationData locData = new MyLocationData. builder (). accuracy (location. getRadius () // set the direction information obtained by the developer, Which is 0-360 clockwise. direction (100 ). latitude (location. getLatitude ()). longpolling (location. getlongpolling ()). build (); mBaiduMap. setMyLocationData (locData); if (isFirstLoc) {isFirstLoc = false; LatLng ll = new LatLng (location. getLatitude (), location. getlongpolling (); Log. d ("jiejie", "Latitude" + location. getLatitude () + "longpolling" + location. getlongpolling () + "location" + location. getCity () + location. getAddrStr (); // TV _location.setText (ll + "detailed location:" + location. getAddrStr () + "City:" + location. getCity (); nowPosition = location. getAddrStr (); MapStatus. builder builder = new MapStatus. builder (); builder.tar get (ll ). zoom (12.0f); mBaiduMap. animateMapStatus (MapStatusUpdateFactory. newMapStatus (builder. build (); }}/ *** add mark */private BitmapDescriptor operator = BitmapDescriptorFactory. fromResource (R. drawable. icon_marka); private Bundle bundle; private void initOverlay () {// TODO Auto-generated method stub // loaded data carInfos. add (new CarInfo ("no alarm", "1", "39.939723", "116.425541", "Wan A111", "9208000", "11 ", "Acc shut down empty cars"); carInfos. add (new CarInfo ("no alarm", "2", "39.942821", "116.369199", "Wan A222", "9208000", "12 ", "Acc shut down empty cars"); carInfos. add (new CarInfo ("no alarm", "3", "31.843025", "117.275366", "Anhui N333", "9208000", "13 ", "ACC shut down empty cars"); carInfos. add (new CarInfo ("no alarm", "4", "31.803025", "117.255366", "Anhui N444", "9208000", "14 ", "ACC shut down empty cars"); carInfos. add (new CarInfo ("alarm available", "5", "30.82943", "116.89586", "Wan WA555", "9208000", "15 ", "Acc does not close"); carInfos. add (new CarInfo ("alarm available", "6", "31.81943", "117.134623", "Wan WA666", "9208000", "16 ", "Acc not closed"); for (CarInfo info: carInfos) {LatLng llLng = new LatLng (Float. parseFloat (info. getLatitude (), Float. parseFloat (info. getlongpolling (); bundle = new Bundle (); bundle. putSerializable ("STATION", info); // MarkerOptions ooA = new MarkerOptions (). position (llLng ). icon (icon ). extraInfo (bundle )//. zIndex (9 ). draggable (true); // ooA. animateType (MarkerAnimateType. grow); OverlayOptions options = new MarkerOptions (). position (llLng ). icon (icon ). extraInfo (bundle ). zIndex (9 ). title (info. getLicenseNumber ()). draggable (true ). animateType (MarkerAnimateType. grow); mBaiduMap. addOverlay (options) ;}}@ Override protected void onPause () {// The Life Cycle of MapView is synchronized with the Activity. When the activity is suspended, MapView must be called. onPause () mMapView. onPause (); super. onPause () ;}@ Override protected void onResume () {// synchronize the life cycle of MapView with Activity. When activity is restored, MapView must be called. onResume () mMapView. onResume (); super. onResume () ;}@ Override protected void onDestroy () {// destroy the mLocClient when exiting. stop (); // close the position layer mBaiduMap. setMyLocationEnabled (false); // The Life Cycle of MapView is synchronized with the Activity. When the activity is destroyed, MapView must be called. destroy () mMapView. onDestroy (); mMapView = null; super. onDestroy (); // reclaim the bitmap resource token. recycle ();}}

 

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.