地图上未显示任何位置更改

时间:2011-11-30 04:45:59

标签: android

我想通过使用GPS编写人员跟踪应用程序,以便通过短信将坐标(纬度和经度)发送到Android手机,并带上坐标值以显示谷歌地图上的准确位置。

工作系统分为2类 1. SMSReceiver类:接收SMS然后将坐标值转换为字符串值为双值

这是我的代码:

package com.google.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver {

public String slat;
public String slng;
public double dLat;
public double dLng;

public double getLat() {
    return this.dLat;
}

public double getLng() {
    return this.dLng;
}

@Override
public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String position1 = "";            
    if (bundle != null) {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
            position1 += msgs[i].getMessageBody().toString();       
        }
        String position2 = position1.substring(position1.indexOf(" ")+1, position1.length());
        slat = position1.substring(position1.indexOf(":")+1, position1.indexOf(" "));
        slng = position2.substring(position2.indexOf(":")+1, position2.indexOf(" "));
        dLat = Double.parseDouble(slat);
        dLng = Double.parseDouble(slng);
    }                         
}
}  

2。跟踪类:从SMS接收器类中获取双倍值以在Google Map上显示位置

这是我的代码:

package com.google.android;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ZoomControls;

public class TrackingActivity extends MapActivity {
/** Called when the activity is first created. */
private LocationManager locationManager;
private LocationListener locationListener;
private MapView mapView;
private MapController mapController;
private Button btnSatelite;
private Button btnStreet;
private ZoomControls zoomControls;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    btnSatelite = (Button)findViewById(R.id.btnSatelite);
    btnSatelite.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
        // TODO Auto-generated method stub
                mapView.setTraffic(false);
                mapView.setSatellite(true);
        }
    }
    );

    btnStreet = (Button)findViewById(R.id.btnStreet);
    btnStreet.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        // TODO Auto-generated method stub
                mapView.setSatellite(false);
                mapView.setTraffic(true);
        }
    }
    );

    zoomControls = (ZoomControls)findViewById(R.id.zoomControls1);
    zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mapController.zoomIn();
            }
    });
    zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mapController.zoomOut();
            }
    });

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);

    mapView = (MapView)findViewById(R.id.mapview1);
    mapController = mapView.getController();

}

private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        SMSReceiver Lat = null;
        SMSReceiver Lng = null;

        /*if (location != null) {
            Toast.makeText(getBaseContext(), "Latitude: " + Lat.getLat() + " Longtitude: " + Lng.getLng(), Toast.LENGTH_SHORT).show();
        }*/

        GeoPoint point = new GeoPoint((int) (Lat.getLat() * 1E6),(int) (Lng.getLng() * 1E6));
        mapController.animateTo(point);
        mapController.setZoom(18);

        ...

        mapView.invalidate();
    }

    ...

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    ...

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

从SMS收到坐标值后,地图上没有显示位置变化。我想这可能是因为Tracking类不能引用SMS接收器类的坐标值。请给我一些建议,我该怎么做才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

基本上我不明白你的代码:我不明白你的SMSReceiver,如果你应该通过短信收到新职位,你应该在你的SMSReceiver和MapActivity之间实现连接。

你在这里基本上做的是设置一个功能,当你的GPS得到一个新的修复时它被调用(当你收到一个新的短信时它不被呼叫):

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);

此外,在你的onLocationChanged()中,你将两个对象设置为null并尝试访问它们的属性,当然这应该会给你一个NullPointerException。

我建议您更改SMSReceiver以便在短信接收时处理听众。