我做了一個AlertDialog
與EditText
,我希望當用戶點擊'Buscar'(positiveButton)時,地圖在AlertDialog
上輸入的地址中顯示Marker
。所以我認爲我需要將數據從這個EditText
傳遞到MapFragment
,以便將Geocoder
與這些數據一起使用。如何使用提示對話框在google maps api v2中搜索地址?
這裏是我的mainActivity.java
與AlertDialog
:
package com.example.alex.testing_map;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MapsActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private String location = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
Fragment fragment = new MapFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frame_content, fragment);
ft.commit();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.add_item:
Intent intent = new Intent(this, AddMail.class);
this.startActivity(intent);
break;
case R.id.search:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dirección");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Buscar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
location = input.getText().toString();
}
});
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.mapa:
fragment = new MapFragment();
FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
ft1.replace(R.id.frame_content, fragment);
ft1.commit();
getSupportActionBar().setTitle(R.string.app_name);
break;
case R.id.help:
fragment = new Help();
FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.replace(R.id.frame_content, fragment);
ft2.commit();
getSupportActionBar().setTitle(item.getTitle());
break;
case R.id.about:
fragment = new About();
FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
ft3.replace(R.id.frame_content, fragment);
ft3.commit();
getSupportActionBar().setTitle(item.getTitle());
break;
case R.id.rate:
Intent face = new Intent(Intent.ACTION_VIEW);
face.setData(Uri.parse("fb://profile/" + "100000914715145"));
startActivity(face);
break;
default:
return true;
}
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
而且我MapFragment.java
:
package com.example.alex.testing_map;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, viewGroup, false);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
SupportMapFragment fragment = new SupportMapFragment();
transaction.replace(R.id.mapView, fragment);
transaction.commit();
fragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
}
}
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setIndoorLevelPickerEnabled(false);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.getUiSettings().setRotateGesturesEnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
// Primer poso les coordenades i estableixo els punts (numerats amb lletres en castellà)
// i després afegeixo els marcadors, posant de localització
// el nom dels punts definits amb anterioritat
LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
LatLng p1 = new LatLng(41.39355, 2.15473);
mMap.addMarker(new MarkerOptions().position(p1).icon(BitmapDescriptorFactory.fromResource(R.drawable.pointer_bici1)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}
}
你的問題的最佳解決方案是https://developers.google.com/places/android-api/placepicker#introduction –
你知道如何獲得地址latlog嗎? –