为什么 mMap.addMarker (..) 和 map.getMyLocation(); 不工作并停止我的应用程序



在sqlite中,相同的方法有效,但是我将数据库更改为Mysql,此方法不起作用!

我从AsyncTask发送arraylist(names(以显示MapsActivity中的方法 和 probleme in ligne
myLocation = mMap.getMyLocation((; 和 mMap.addMarker(new MarkerOptions((.position(dz(.title("welcome to"((.showInfoWindow((;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public void mark(Double latitude, Double longitude) {
LatLng dz = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(dz).title("welcome to")).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dz, 16));
}
public float distanceBetween(Double latitude, Double longitude) {
float result[] = new float[10];     
Location myLocation = mMap.getMyLocation();
Double mylatitude = myLocation.getLatitude();
Double mylongitude = myLocation.getLongitude();
myLocation.distanceBetween(mylatitude, mylongitude, latitude, longitude, result);
return result[0];
}
public void show ( ArrayList names) {
ArrayList<String> arrList = new ArrayList<>();
arrList = names;
int c = arrList.size();
Float t[] = new Float[10];
Double company[] = new Double[3];
if (c > 0) {
int n = 0;
int i = 0;
while (c > 0) {
Double latitude = Double.valueOf(arrList.get(n));
Double longitude = Double.valueOf(arrList.get(n + 1));
Double price = Double.valueOf(arrList.get(n + 2));
t[i] = distanceBetween(latitude, longitude);
if (n == 0) {
company[0] = latitude;
company[1] = longitude;
company[2] = price;
}
if ((i > 0) && (t[i] < t[i - 1])) {
t[i] = t[i - 1];
company[0] = latitude;
company[1] = longitude;
company[2] = price;
}
i = i + 1;
n = n + 3;
c = c - 3;
}
mark(company[0], company[1]);

} else {
Toast T = Toast.makeText(this, "product is not available ", Toast.LENGTH_SHORT);
T.show();
}
Toast.makeText(this, "price of product = " + company[2], Toast.LENGTH_LONG).show();
}
}
public class Parser2 extends AsyncTask<Void,Void,Integer> {
Context c;
String data;

ArrayList<String> names=new ArrayList<>();
public Parser2(Context c, String data) {
this.c = c;
this.data = data;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer==1)
{
MapsActivity m =new MapsActivity();
m.show(names);
}else {
Toast.makeText(c,"Unable to Parse2",Toast.LENGTH_SHORT).show();
}
}
private int parse()
{
try
{
JSONArray ja=new JSONArray(data);
JSONObject jo=null;
names.clear();
int i;
for( i=0;i<ja.length();i++)
{
jo=ja.getJSONObject(i);
String latitude=jo.getString("latitude");
String longitude=jo.getString("longitude");
String price=jo.getString("price");
names.add(latitude);
names.add(longitude);
names.add(price);}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}

似乎问题的原因是因为 getMyLocation(( 为空,并且算法期望它不为空。请确保此对象包含值。您可以使用此代码检查用户的最后一个位置:

LocationManager service = (LocationManager) 
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());

希望这些信息对您有所帮助,并祝您的项目好运!

相关内容

  • 没有找到相关文章

最新更新