Saturday, February 22, 2014

Baidu maps, location-based services can not start request




Here is my code, I want to get after their geographical location, are displayed in the text domain, but the method is called requestLocation Each time the return value is 1, the location service does not start, please help me find
Great God


 package com.example.mymap; 

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MyPosition extends Activity {

private TextView latitudeText;
private TextView longtitudeText;
private TextView addressText;


public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener ();

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.myposition);



latitudeText = (TextView) this.findViewById (R.id.latitude);
longtitudeText = (TextView) this.findViewById (R.id.longtitude);
addressText = (TextView) this.findViewById (R.id.address);


mLocationClient = new LocationClient (this); / / declare LocationClient class
mLocationClient.registerLocationListener (myListener); / / Register the listener function
LocationClientOption option = new LocationClientOption ();
option.setOpenGps (true);
option.setAddrType ("detail");
option.setCoorType ("gcj02");
option.setScanSpan (5000);
option.setPoiNumber (5); / / returns the maximum number of
POIoption.setPoiDistance (1000); / / poi inquiries from
option.setPoiExtraInfo (true); details / / if need POI phone number and address
mLocationClient.setLocOption (option);

mLocationClient.start ();



}

public void getPositionInfo (View view) {
if (mLocationClient! = null && mLocationClient.isStarted ())
mLocationClient.requestLocation ();
else
Log.d ("LocSDK_2.0_Demo1", "locClient is null or not started");
}




private class MyLocationListener implements BDLocationListener {

@ Override
public void onReceiveLocation (BDLocation location) {

if (location == null | | location.getLocType () == 162) {
Toast.makeText (MyPosition.this, "positioning failed", 1) show ();.
} Else {
Log.i ("location", "get a location");
latitudeText.setText ("dimension position" + location.getLatitude ());
longtitudeText.setText ("precision position" + location.getLongitude ());
addressText.setText (location.getAddrStr ());
}



}

@ Override
public void onReceivePoi (BDLocation arg0) {
/ / TODO Auto-generated method stub

}}




}




<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Baidu Maps Demo official suggestion you download to see
Reply:
Solved yet landlord? I also encountered the same problem
Reply:
reference to the second floor q_750750 reply:
solved yet landlord? I also encountered the same problems


You configure the service in the manifest file it? I just did not configure the service. . .
Reply:
Configuration that it? When I configured it can not successfully start ah
Reply:
locationClient = new LocationClient (getApplicationContext ());
Put your this to getApplicationContext () on the line.
Reply:
Put this to getApplicationContext (), but still not hey. . Who will help ah.
Reply:
reference to the 6th floor zhengrongjun reply:
put this to getApplicationContext (), but still not hey. . Who will help ah.


Code is no problem, my problem is in the service of this component is not configured in the configuration file
Reply:
references, 4th Floor ox110202011011 reply:
is configured that it? When I configured it can not successfully start ah
I have configured, and also returns 1. Later found not start, see if you do not start
Reply:
references, 5th Floor a88508563 reply:
locationClient = new LocationClient (getApplicationContext ());
Put your this to getApplicationContext () on the line.

Thank you, this way she can. However, I still do not understand a bit: Engineering (1) I do the test is used, there is only one MainActivity when I use this, to successfully obtain location information, Engineering (2) there are multiple Activity, obtain location information The Activity is not MainActivity. Then again this could not get to, but changed getApplicationContext () successfully access to the location information ... What is the reason? I hope you can answer ... Thanks again!
Reply:
reference to the 9th floor xu_yuan_zhi reply:
Quote: references, 5th Floor a88508563 reply:

locationClient = new LocationClient (getApplicationContext ());
Put your this to getApplicationContext () on the line.

Thank you, this way she can. However, I still do not understand a bit: Engineering (1) I do the test is used, there is only one MainActivity when I use this, to successfully obtain location information, Engineering (2) there are multiple Activity, obtain location information The Activity is not MainActivity. Then again this could not get to, but changed getApplicationContext () successfully access to the location information ... What is the reason? I hope you can answer ... Thanks again!

Encounter with the main layer is consistent, adjusting the twelve days the rub ...
Figure out the problem yet? I looked under the getApplicationContext (), and this difference, about the life cycle, but did not understand
Reply:
reference to the 9th floor xu_yuan_zhi reply:
Quote: references, 5th Floor a88508563 reply:

locationClient = new LocationClient (getApplicationContext ());
Put your this to getApplicationContext () on the line.

Thank you, this way she can. However, I still do not understand a bit: Engineering (1) I do the test is used, there is only one MainActivity when I use this, to successfully obtain location information, Engineering (2) there are multiple Activity, obtain location information The Activity is not MainActivity. Then again this could not get to, but changed getApplicationContext () successfully access to the location information ... What is the reason? I hope you can answer ... Thanks again!

Baidu Class Reference inside wrote:
public LocationClient (Context) / / must be declared in the main thread
Is a relationship with this, this is not the main thread?
Reply:
This should be done only when an Activity, which this is the main thread, and when I do have multiple Activity when the Activity is another entrance, that is, at this time and then this, it is not in the main the thread ... do not know is not like that, so I do understand it first, if you have any good explanation, Could you tell me ... thank you!
Reply:
reference to the 11th floor eznanke reply:
Quote: references to the 9th floor xu_yuan_zhi reply:

Quote: references, 5th Floor a88508563 reply:

locationClient = new LocationClient (getApplicationContext ());
Put your this to getApplicationContext () on the line.

Thank you, this way she can. However, I still do not understand a bit: Engineering (1) I do the test is used, there is only one MainActivity when I use this, to successfully obtain location information, Engineering (2) there are multiple Activity, obtain location information The Activity is not MainActivity. Then again this could not get to, but changed getApplicationContext () successfully access to the location information ... What is the reason? I hope you can answer ... Thanks again!

Baidu Class Reference inside wrote:
public LocationClient (Context) / / must be declared in the main thread
Is a relationship with this, this is not the main thread?


This should be done only when an Activity, which this is the main thread, and when I do have multiple Activity when the Activity is another entrance, that is, at this time and then this, it is not in the main the thread ... do not know is not like that, so I do understand it first, if you have any good explanation, Could you tell me ... thank you!
Reply:
reference to the 13th floor xu_yuan_zhi reply:
only one Activity time, which this is the main thread, and when I do, when there are multiple Activity entrance Activity is another one, that is to say, at this time and then this, it is not in the main thread of the ... do not know if it is like this, so I do understand it first, if you have any good explanation, trouble You tell me ... thank you!

Official sample Baidu's what you saw, positioning inside with layers that example is this, rather than getapplicationcontext
Reply:
android: name = "com.baidu.location.f"
android: enabled = "true"
android: process = ": remote"
android: exported = "false">

android: exported = "false" />










java files added option.setServiceName ("com.baidu.location.service");
Unable to start service to solve the problem. Bag with built-in service

No comments:

Post a Comment