目前,我可以在代码中创建的第一个点显示一个点(pointStatie),但我想同时显示两个点。我想显示我所在的点以及我从另一个类传递坐标的另一个点。
我目前的代码是:
package aexp.elistcbox;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Criteria;
import android.util.Log;
import android.widget.Toast;
import android.location.Geocoder;
import android.location.Address;
import java.util.List;
import java.util.Locale;
import java.io.*;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
public class GPSLocatorActivity extends MapActivity
{
public MapView mapView;
public MapController mapController;
public LocationManager locationManager;
public LocationListener locationListener;
public String best;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setSatellite(false);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(13);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
double longitudine = extras.getDouble("long");
double latitudine = extras.getDouble("lat");
((GPSLocationListener) locationListener).setLongitudine(longitudine);
((GPSLocationListener) locationListener).setLatitudine(latitudine);
Log.e("longitudine", "* " + ((GPSLocationListener) locationListener).setLongitudine(longitudine));
Log.e("latitudine", "* " + ((GPSLocationListener) locationListener).setLatitudine(latitudine));
}
String provider = null;
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
}
if(provider != null) {
locationManager.requestLocationUpdates(provider, 60000, 15, locationListener);
}
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = locationManager.getBestProvider(crit, false);
locationManager.requestLocationUpdates(best, 60000, 15, locationListener);
Location loc = locationManager.getLastKnownLocation(provider);
locationListener.onLocationChanged(loc);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onResume() {
super.onResume();
//locationManager.requestLocationUpdates(best, 10000, 1, locationListener);
Toast.makeText(this, "GPS tracking started",
Toast.LENGTH_SHORT).show();
// Start location updates; 5s/5m
LocationManager locManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
60000, 15, locationListener);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locManager.getBestProvider(crit, true);
Location loc = locManager.getLastKnownLocation(provider);
}
@Override
protected void onPause() {
super.onPause();
//locationManager.removeUpdates(locationListener);
Toast.makeText(this, "GPS tracking stopped",
Toast.LENGTH_SHORT).show();
LocationManager locManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
locManager.removeUpdates(locationListener);
}
private class GPSLocationListener implements LocationListener
{
double longitudine;
double latitudine;
public double setLongitudine(double longitudine) {
return this.longitudine = longitudine;
}
public double setLatitudine(double latitudine) {
return this.latitudine = latitudine ;
}
public void onLocationChanged(Location location) {
Toast.makeText(GPSLocatorActivity.this, "Te plimbi",
Toast.LENGTH_SHORT).show();
if (location != null) {
Log.e("longitudine", "*2 " + longitudine);
Log.e("latitudine", "*2 " + latitudine);
GeoPoint pointStatie = new GeoPoint(
(int) (latitudine * 1E6),
(int) (longitudine * 1E6));
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
// afisaza coordonatele
/*
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
*/
mapController.animateTo(point);
mapController.animateTo(pointStatie);
mapController.setZoom(13);
//mapView.invalidate();
// add marker for people point
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
mapOverlay.setPointToDraw(pointStatie);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
String addressStatie = ConvertPointToLocation(pointStatie);
Toast.makeText(getBaseContext(), addressStatie, Toast.LENGTH_SHORT).show();
}
}
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
class MapOverlay extends Overlay
{
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// convert point to pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mark_red);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled (String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}