Baidu Maps on Android: Passkey Doesn't Work for Finding Location

I am building an Android app for a Chinese client and they need maps integration, so google maps is not an option as all google services are blocked in China. I am trying to use Baidu maps which are called Baidu LBS cloud service (cloud services).

Getting a basemap without overlays to work with was relatively easy. The process is described here (in Chinese, but the code speaks for itself if you don't understand the language). Downloading the latest Baidu Android SDK (v3.2.0 at the time of writing) and integrating it into an Eclipse project as a library was no problem, but don't trust the documentation in this link too much, even if it's official. Their examples often contain code that doesn't even compile. For example, the name of the .jar file is completely different from what you see in the screenshot.

Oh, and also their .jar library is confusing, which is very annoying to work with: --(

I needed to register for a Baidu account and go to the control center to generate a key. To create an access key ("ak") for mobile devices, you need to enter the SHA1 fingerprint of the keystore that your app signs, followed by the package name specified in your manifest. Then I added the generated key to my manifest under the tag

<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="xxx...xxx" />

      

Then I copied the code from my CloudSearchActivity sample project because I have specific coordinates that I would like to display. I have implemented the CloudListener interface as shown below:

@Override
public void onGetSearchResult(final CloudSearchResult result, final int error)
{
    Log.w("onGetSearchResult", "status=" + result.status + ". size=" + result.size + ". total=" + result.total + ". error=" + error);
    if(null != result && null != result.poiList && 0 < result.poiList.size())
    {
      mBaiduMap.clear();
      final BitmapDescriptor bitmapDescriptor=BitmapDescriptorFactory.fromResource(R.drawable.icon_address_grey);
      LatLng latitudeLongitude;
      LatLngBounds.Builder builder=new Builder();
      for(final CloudPoiInfo info : result.poiList)
      {
        latitudeLongitude=new LatLng(info.latitude, info.longitude);
        final OverlayOptions overlayOptions=new MarkerOptions().icon(bitmapDescriptor).position(latitudeLongitude);
        mBaiduMap.addOverlay(overlayOptions);
        builder.include(latitudeLongitude);
      }
      final LatLngBounds bounds=builder.build();
      MapStatusUpdate mapStatusUpdate=MapStatusUpdateFactory.newLatLngBounds(bounds);
      mBaiduMap.animateMapStatus(mapStatusUpdate);
    }
}

      

And I added some code to run the request (also copied from my sample project):

  @Override
  public View onCreateView(final LayoutInflater layoutInflater, final ViewGroup viewGroup,
    final Bundle savedInstanceState)
  {
    // initialize needs to be called
    SDKInitializer.initialize(getApplication());
    CloudManager.getInstance().init(MyFragment.this);

    view=(ViewGroup)layoutInflater.inflate(R.layout.fragment_map, viewGroup, false);

    mMapView=(MapView)view.findViewById(R.id.baiduMapView);
    mBaiduMap=mMapView.getMap();

    NearbySearchInfo info=new NearbySearchInfo();
    info.ak="xxx...xxx";
    info.geoTableId=12345;
    info.tags="";
    info.radius=30000;
    info.location="116.403689,39.914957";
    CloudManager.getInstance().nearbySearch(info);

    return view;
  }

      

Unfortunately, I keep getting the status value 102 from the server (according to this API page , which means STATUS_CODE_SECURITY_CODE_ERROR

. Now I don't know, I know what to do. Things I don't understand:

  • Why do I need to repeat my access key ("ak") when building a query? Is this not enough in the manifest?
  • What is this "geoTableId" value in the request that should be?

Any ideas?

+3


source to share


1 answer


After many hours of research, I've made some progress on open-ended questions.

  • The reason for the "ak" field in the cloud search query is not duplication, it is actually a different access key. Somewhere in a hidden place, Baidu says mobile access keys won't work for these cloud searches, you need ak "for the server." So the solution is to go back to Baidu's control center and create another key for the server. This key must be used in the request, while the mobile key must remain in the manifest.

  • geoTableId is your account ID, not like access keys. This is the (currently) 5 digit number to get from the Baidu Command Center. Other keys were generated in the "API 控制台" tab (API control panel), but for the geoTableId you need to switch to the "数据 管理" tab (data management). There, I guess I had to click the "创建" (~ create) button in the upper left corner, then enter the name, select "是" (yes) where they ask if this is for release (not sure about this translation) , and then press "保存" (save). After that, your newly generated number is displayed in the upper field in parentheses behind the name that you have chosen right now.



These steps allowed me to send "success" requests where the server responds with a status of 0 (STATUS_CODE_SUCCEED). However, so far all the responses I receive are empty, I have yet to find a query that gives a non-empty response. If anyone succeeds in doing this, please let me know!

+1


source







All Articles