我开发了一个 Android 应用程序,该应用程序使用 Google Maps API v2 来显示显示用户位置的地图、不同的其他点(商店......)、一个跟踪蓝点,代表用户移动和指向特定位置时的方向.
所有这些在某些设备上都能正常工作,但在其他设备上却不行,我不知道为什么?
适用于 Android 4.2.2、4.3、2.3.7
但不适用于 Android 2.3.6,例如 4.0.1。
是否与其他设备上未安装的 Google Play 服务相关?
我的代码有问题吗?
这是我的代码:
package com.app.connexion;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import com.datatype.AppData;
import com.datatype.OrderInfo;
import com.datatype.ShopInfo;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.utils.GMapV2Direction;
import com.utils.RequestTask;
import com.utils.RequestTask1;
import com.utils.RequestTaskDelegate;
import com.utils.WebServiceMethod;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class DetailOrder extends FragmentActivity implements RequestTaskDelegate{
/** Called when the activity is first created. */
String request;
String basket;
ImageButton checkout;
ProgressDialog prog;
GoogleMap mMap;
GMapV2Direction md;
String toaddr = "";
LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);
String orderid,gps;
PolylineOptions rectLine;
ArrayList<ShopInfo> shops;
ImageButton posbtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.detailorderone);
checkout = (ImageButton)findViewById(R.id.imageButton1);
orderid = getIntent().getStringExtra("orderid");
gps = getIntent().getStringExtra("others");
posbtn = (ImageButton)findViewById(R.id.imageButton2);
posbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mMap.clear();
sendGPS();
positionredraw();
}
});
checkout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(),DetailProductVC.class);
intent.putExtra("basket", basket);
intent.putExtra("orderid", orderid);
startActivity(intent);
}
});
prog = ProgressDialog.show(this, "", "Processing...",true,false);
RequestTask task = new RequestTask();
task.delegate = this;
request = WebServiceMethod.kCommandeInfosMethode;
task.execute(request,orderid);
getCurrentPosition();
}
public void positionredraw()
{
//prog = ProgressDialog.show(this, "", "Processing...",true,false);
viewmap1();
}
public void getCurrentPosition()
{
mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
mMap.setMyLocationEnabled(true);
UiSettings ui = mMap.getUiSettings();
ui.setMyLocationButtonEnabled(false);
mMap.getMyLocation();
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
fromPosition = new LatLng(location.getLatitude(),location.getLongitude());
/*
mMap.clear();
sendGPS();
positionredraw();
*/
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());
fromPosition = new LatLng(location.getLatitude(),location.getLongitude());
//fromPosition = new LatLng(48.8966175,2.327197);
Log.d("current",message);
}
}
public void sendGPS()
{
Thread t;
t = new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
try{
sendGPSData();
}catch(Exception e)
{
}
}
});
t.start();
}
public void sendGPSData()
{
SharedPreferences shared = getSharedPreferences("connexion",MODE_PRIVATE);
String shopperid = shared.getString("shopperid", "");
HttpClient client = new DefaultHttpClient();
HttpPost post;
post = new HttpPost(WebServiceMethod.serverurl);
Log.d("gps", "send gps");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("key", WebServiceMethod.key));
nameValuePairs.add(new BasicNameValuePair("secret", WebServiceMethod.secret));
nameValuePairs.add(new BasicNameValuePair("shopperid", shopperid));
nameValuePairs.add(new BasicNameValuePair("latitude", String.valueOf(fromPosition.latitude)));
nameValuePairs.add(new BasicNameValuePair("longitude", String.valueOf(fromPosition.longitude)));
nameValuePairs.add(new BasicNameValuePair("act", WebServiceMethod.kUpdateShopperStatusMethode));
try{
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
HttpResponse response = client.execute(post);
//response = client.execute(post);
HttpEntity resEntity = response.getEntity();
String str = EntityUtils.toString(resEntity);
Log.d("gpsResult",str);
}catch(Exception e)
{
}
}
public void viewmap1()
{
LatLng from,to;
//prog.dismiss();
rectLine = new PolylineOptions().width(10).color(Color.BLUE);
mMap.addMarker(new MarkerOptions().position(fromPosition).title("Moi"));
//mMap.addMarker(new MarkerOptions().position(toPosition).title("B"));
String[] gpss = gps.split("#");
from = fromPosition;
to = null;
for(int i = 0; i < gpss.length - 1;i++)
{
to = new LatLng(Double.parseDouble(gpss[i].split(",")[0]),Double.parseDouble(gpss[i].split(",")[1]));
viewmap(from,to);
from = to;
}
int len = gpss.length - 1;
viewmap(from,new LatLng(Double.parseDouble(gpss[len].split(",")[0]),Double.parseDouble(gpss[len].split(",")[1])));
ArrayList<OrderInfo> orders = ((AppData)this.getApplication()).orders;
for(int i = 0; i < gpss.length;i++)
{
OrderInfo order = orders.get(i);
to = new LatLng(Double.parseDouble(gpss[i].split(",")[0]),Double.parseDouble(gpss[i].split(",")[1]));
mMap.addMarker(new MarkerOptions().position(to).title(order.name));
}
LatLng coordinates = new LatLng((fromPosition.latitude + to.latitude)/2, (fromPosition.longitude + to.longitude)/2);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 13));
mMap.addPolyline(rectLine);
try{
for(int i = 0;i < shops.size();i++)
{
mMap.addMarker(new MarkerOptions().position(shops.get(i).pos).title(shops.get(i).name));
}
}catch(Exception e)
{
}
}
public void viewmap(LatLng from,LatLng to)
{
SharedPreferences shared = getSharedPreferences("connexion",MODE_PRIVATE);
String type = shared.getString("type", "");
md = new GMapV2Direction(type);
//LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);
Document doc;
if(type.equals("0"))
doc = md.getDocument(from, to, GMapV2Direction.MODE_WALKING);
else
doc = md.getDocument(from, to, GMapV2Direction.MODE_DRIVING);
/*
int duration = md.getDurationValue(doc);
String distance = md.getDistanceText(doc);
String start_address = md.getStartAddress(doc);
String copy_right = md.getCopyRights(doc);
*/
ArrayList<LatLng> directionPoint = md.getDirection(doc);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
}
@Override
public void backgroundActivityComp(String response) {
// TODO Auto-generated method stub
if(request.equals(WebServiceMethod.kCommandeInfosMethode))
{
try {
JSONObject json = new JSONObject(response);
String str = json.getString("Results");
if(!str.equals("false"))
{
json = new JSONObject(str);
JSONObject gps = new JSONObject(json.getString("addr"));
JSONObject gps1 = new JSONObject(gps.getString("gps"));
toPosition = new LatLng(Double.parseDouble(gps1.getString("latitude")),Double.parseDouble(gps1.getString("longitude")));
toaddr = gps.getString("name");
basket = json.getString("basket");
SharedPreferences shared = getSharedPreferences("connexion",MODE_PRIVATE);
RequestTask task = new RequestTask();
task.delegate = this;
request = WebServiceMethod.kShipInfosMethode;
task.execute(request,shared.getString("shipid", ""));
return;
}
}catch(Exception e)
{
e.printStackTrace();
prog.dismiss();
}
return;
}
if(request.equals(WebServiceMethod.kShipInfosMethode))
{
try{
prog.dismiss();
JSONObject json = new JSONObject(response);
shops = new ArrayList<ShopInfo>();
if(!json.getString("Results").equals("false"))
{
json = new JSONObject(json.getString("Results"));
//shops = new ArrayList<ShopInfo>();
JSONArray jsons = new JSONArray(json.getString("shops"));
for(int i = 0; i < jsons.length();i++)
{
ShopInfo shop = new ShopInfo();
json = new JSONObject(jsons.get(i).toString());
shop.name = json.getString("name");
shop.pos = new LatLng(Double.parseDouble(json.getString("latitude")),Double.parseDouble(json.getString("longitude")));
shops.add(shop);
}
viewmap1();
return;
}
}
catch(Exception e)
{
prog.dismiss();
return;
}
}
}
}