android和地图中的纬度和经度变化未显示
package net.learn2develop.GoogleMaps;
import java.io.IOException;
import java.util.*;
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 com.google.android.maps.MapView.LayoutParams;
import android.app.LocalActivityManager;
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.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MapsActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
double latPoint, lngPoint;
LocationManager myManager;
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Log.e("address", add);
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());
mc.setZoom(10);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class myLocationListener implements LocationListener {
public void ListLocationUpdater() {
}
@Override
public void onLocationChanged(Location loc) {
if (myManager != null) {
// List list = myManager.getAllProviders();
String param = (String) myManager.getProviders(true).get(0);
loc = myManager.getLastKnownLocation(param);
if (loc != null) {
latPoint = loc.getLatitude();
lngPoint = loc.getLongitude();
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
mc.animateTo(p); Log.e("RootDrawApplication",String.valueOf(latPoint)+" , "+String.valueOf(lngPoint));
} else
Log.e("GoogleMaps ", "Error: Location is null");
} else
Log.e("GoogleMaps ", "Error: Location Manager is null");
}
@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
}
}
}
答案 0 :(得分:2)
创建后台线程并检查那里的gps数据。 并使用LocationListener:
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location l) {
TextView tv = new TextView(gpstracker.this);
tv.setText("lat: " + l.getLatitude() + "\nlon: " + l.getLongitude());
setContentView(tv);
}
...
This也可以帮助......
答案 1 :(得分:0)
使用以下代码向位置管理器添加位置监听器:
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new myLocationListener());
这样,myLocationListener将实现locationListener接口并更改onLocationChangedFunction中的Location。
public class myLocationListener implements LocationListener {
public ListLocationUpdater() {
}
@Override
public void onLocationChanged(Location location) {
//assign location here
}
@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
}
}