我创建了一个应用程序,在地图上显示我当前的位置。我想在onInfoWindowClick方法下显示toast消息中的位置。但是吐司不工作。提前感谢您的帮助!
下面是我的代码:public class SearchProviderLocation extends Activity implements LocationListener,OnInfoWindowClickListener {
class MyInfoWindowAdapter implements InfoWindowAdapter{
private final View myContentsView;
MyInfoWindowAdapter(){
myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
}
@Override
public View getInfoContents(Marker marker) {
TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
return myContentsView;
}
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}
final int RQS_GooglePlayServices = 1;
TextView tvLocInfo;
GoogleMap map;
private LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
String provider;
double latitude;
protected double longitude;
protected boolean gps_enabled,network_enabled;
private String val,val1;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
boolean isGPSEnabled ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_provider_location);
try {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setInfoWindowAdapter(new MyInfoWindowAdapter());
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
map.clear();
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
mp.snippet("Address:"+" "+" "+addresses.get(0).getFeatureName()+" "+addresses.get(0).getAdminArea()+" "+addresses.get(0).getLocality()+" "+addresses.get(0).getPostalCode()+""+addresses.get(0).getCountryCode());
mp.title(addresses.get(0).getLocality()+", "+addresses.get(0).getCountryName());
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 14));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"GooglePlayServicesAvailable",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added@" + point.toString());
Marker newMarker = map.addMarker(new MarkerOptions()
.position(point)
.snippet(point.toString()));
newMarker.setTitle(newMarker.getId());
}
@Override
public void onInfoWindowClick(Marker marker)
{
marker.getTitle();
final String ssid = marker.getTitle();
Toast.makeText(getBaseContext(),"You click @" + ssid,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}}
试试下面的代码:
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener()
{
@Override public void onInfoWindowClick(Marker arg0) {
final String ssid = arg0.getTitle();
Toast.makeText(SearchProviderLocation.this,"You click @" + ssid,Toast.LENGTH_SHORT).show();
}
});