gpt4 book ai didi

android - 在Android中放置点击对话框?

转载 作者:行者123 更新时间:2023-11-30 04:13:32 24 4
gpt4 key购买 nike

我在 EditText 中给出 map 搜索位置,然后在 map 中显示该位置。我需要的是这个,我想在我们点击 map 中的地方时显示一个对话框?这样我就可以将该位置移动到 Android ListView

package com.example.tutorials;

import java.io.IOException;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;

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.MapView.LayoutParams;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class GoogleMap extends MapActivity
{

MapView mapView;

/** Called when the activity is first created. */
@Override
protected boolean isRouteDisplayed()
{
return false;
}

public void changeMap(String area)
{

mapView = (MapView) findViewById(R.id.mapview);
MapController mc=mapView.getController();


GeoPoint myLocation=null;

double lat = 0;
double lng = 0;
try
{

Geocoder g = new Geocoder(this, Locale.getDefault());

java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){

Toast.makeText(GoogleMap.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();

}
else{
Toast.makeText(GoogleMap.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(GoogleMap.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {


EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(GoogleMap.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
GoogleMap.this.changeMap(area);


}
});

mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
mapController.setZoom(14);

LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);

}

}

请看一次,让我知道我可以在哪里更改上面的代码。

最佳答案

要显示对话框,请遵循以下代码:

public class MapViewer extends MapActivity {

MapView mapView;

MapController mapController;

GeoPoint mgeoPoint;

Drawable marker;

MyLocationOverlay mLocationOverlay;

MotionEvent e;


@Override
protected void onCreate(Bundle icicle) {

// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);

mapView = (MapView) findViewById(R.id.mapView);

mapController = mapView.getController();

mapView.setBuiltInZoomControls(true);

mapView.setStreetView(true);


marker = getResources().getDrawable(R.drawable.pushpin);

marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());

mapView.getOverlays().add(new MapOverlay(marker));


mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);

setViewLocation();
}


@Override
protected Dialog onCreateDialog(int id) {

// TODO Auto-generated method stub

switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
R.drawable.ic_launcher).setPositiveButton("Yes",
new OnClickListener() {

@Override
public void onClick(DialogInterface dialog , int which) {

// TODO Auto-generated method stub

}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {

@Override
public void onClick(DialogInterface dialog , int which) {

// TODO Auto-generated method stub

}
})

.setOnCancelListener(new OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {

// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}

}).create();


default:
break;
}
return null;
}


private void setViewLocation() {



String[] coordinates = { "22.716221", "75.896816" };

double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

mapController.animateTo(mgeoPoint);
mapController.setZoom(15);

mapView.invalidate();

}


@Override
protected boolean isRouteDisplayed() {

// TODO Auto-generated method stub
return false;
}


Point scrPoint;


private GeoPoint getPoint(double lat , double lon) {

return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}


class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {

List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();


public MapOverlay(Drawable defaultMarker ) {

super(defaultMarker);

ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
"IN", "India"));

populate();

}


@Override
protected boolean onTap(int index) {

switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}

return true;
}


String add = "";

List<Address> add_List = new ArrayList<Address>();


private void getAddress() {

add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);

}


@Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}


@Override
public int size() {
return ListofGeopoints.size();
}
}
}

关于android - 在Android中放置点击对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com