package xzx.xzx;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import com.amelie.driving.DrivingDirections;
import com.amelie.driving.DrivingDirections.IDirectionsListener;
import com.amelie.driving.DrivingDirections.Mode;
import com.amelie.driving.impl.DrivingDirectionsGoogleKML;
import com.amelie.driving.impl.RouteImpl;
import com.amelie.driving.Route;
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.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class MyMap extends MapActivity implements IDirectionsListener{
/** Called when the activity is first created. */
final Context context=this;
final static int MYLOCATION=Menu.FIRST;
final static int SEARCH=Menu.FIRST+1;
final static int NAVIGATE=Menu.FIRST+2;
final static int MAPMODE=Menu.FIRST+3;
public static int flag=1;
private Resources resources;
public static MapView mapView;
public static MapController mapController;
private static ImageButton image;
public static EditText et;
public static EditText et1;
public static TextView tv;
public static Button bt;
public ScrollView sv;
public static RouteImpl routeimple = null;
public Mode mode;
private MapOverLay mapoverlay;
public GeoPoint mygeopoint;
public Geocoder geo ;
public static int flag1=0;
//private static final String BASE_URL = "http://maps.google.com/maps?f=d&hl=en";
private MyRouteOverLay myRouteOverLay;
public Timer time;
public TimerTask task;
public int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapview);
image=(ImageButton)findViewById(R.id.imageButton1);
et=(EditText)findViewById(R.id.editText1);
et.setText("西安工业大学");
et1=(EditText)findViewById(R.id.editText2);
tv=(TextView)findViewById(R.id.textView1);
bt=(Button)findViewById(R.id.button1);
sv=( ScrollView)findViewById(R.id.scrollview1);
image.setVisibility(View.GONE);
et.setVisibility(View.GONE);
et1.setVisibility(View.GONE);
tv.setVisibility(View.GONE);
sv.setVisibility(View.GONE);
bt.setVisibility(View.GONE);
// bt.setVisibility(View.GONE);
myRouteOverLay=new MyRouteOverLay(0);
geo = new Geocoder(this, Locale.CHINA);
resources = this.getResources();
mapView.setBuiltInZoomControls(true);//显示缩放控件
mapController = mapView.getController();
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager)getSystemService(serviceString);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
// Address myaddress=getLocation("西安工业大学");
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
mygeopoint= new GeoPoint(lat.intValue(), lng.intValue());
ShowSelectPlace(mygeopoint);
locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(flag1==0)
{
tv.setVisibility(View.VISIBLE);
sv.setVisibility(View.VISIBLE);
tv.setTextColor(Color.BLACK);
float f=(float) 13.0;
tv.setTextSize(f);
tv.setHorizontallyScrolling(true);
tv.setText(MyRouteOverLay.ShowRoute(routeimple));
flag1=1;
}
else
{
flag1=0;
sv.setVisibility(View.GONE);
tv.setVisibility(View.GONE);
}
}
});
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(flag==1)
{
String search=et.getText().toString();
image.setVisibility(View.GONE);
et.setVisibility(View.GONE);
ShowSearchPlace(getLocation(search));
}
else
{
//time.schedule(task,1000,3000);
bt.setVisibility(View.VISIBLE);
DrivingDirectionsGoogleKML ddgkml=new DrivingDirectionsGoogleKML();
String start=et.getText().toString();
String end=et1.getText().toString();
if(start.equals(""))Toast.makeText(context, "请选择起点", Toast.LENGTH_LONG).show();
else if(end.equals(""))Toast.makeText(context, "请选择终点", Toast.LENGTH_LONG).show();
else
{
if(MapOverLay.gp0!=null&&MapOverLay.gp1!=null)
{
ddgkml.driveTo(MapOverLay.gp0, MapOverLay.gp1, DrivingDirections.Mode.DRIVING,(IDirectionsListener) context);
et.setVisibility(View.GONE);
et1.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
else
{
Address startaddress=getLocation(start);
Address endaddress=getLocation(end);
Double lat =startaddress.getLatitude()*1E6;
Double lng = startaddress.getLongitude()*1E6;
GeoPoint strartpoint = new GeoPoint(lat.intValue(), lng.intValue());
lat =endaddress.getLatitude()*1E6;
lng = endaddress.getLongitude()*1E6;
GeoPoint endpoint = new GeoPoint(lat.intValue(), lng.intValue());
ddgkml.driveTo(strartpoint, endpoint, DrivingDirections.Mode.DRIVING,(IDirectionsListener) context);
et.setVisibility(View.GONE);
et1.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
flag=1;
}
}
}
});
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private final LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
ShowPlace(location,11);
//GClientGeocoder.getLatLng(address, callback);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
public boolean onCreateOptionsMenu(Menu menu)//创建菜单
{
menu.add(0,MYLOCATION,0,"我的位置");
menu.add(0,SEARCH,1,"搜索");
menu.add(0,NAVIGATE,1,"导航");
menu.add(0,MAPMODE,1,"地图模式");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)//处理菜单选择事件
{
switch(item.getItemId())
{
case MYLOCATION:
ShowSelectPlace(mygeopoint);
return true;
case SEARCH:
flag=1;
image.setVisibility(View.VISIBLE);
et.setVisibility(View.VISIBLE );
return true;
case NAVIGATE:
flag=0;
et.setVisibility(View.VISIBLE);
et1.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
// Address add
- 1
- 2
前往页