我在tab活动中的地图片段中有按钮,如图所示,但我不知道在哪里实现此按钮的onClickListener以及onLocationChanged,onConnectionSuspened等的其他Map函数请帮助!!
已修改 添加代码
MapsActivity.Java
public class MapsActivity extends ActionBarActivity implements
AsyncResponse, View.OnClickListener,
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
protected void onCreate(Bundle savedInstanceState) {
Log.i("okkk", " ho jamaaloooo ");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent intent = getIntent();
newString = intent.getLongExtra("user_id", newString);
btn_rest = (Button) findViewById(R.id.btn_rest);
btn_rest.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// getPars
Log.i("okk", "Click ");
HashMap postData = new HashMap();
PostResponseAsyncTask task = new PostResponseAsyncTask(this, postData);
task.execute("http://ashna.netau.net/A_location.php");
}
TabACtivity.java 从这里调用地图片段
public Fragment getItem(int position) {
Log.i("okkk", "Position: " + position);
try {
switch (position) {
case 0: {
return PlaceholderFragment.newInstance(position + 1);
}
case 1: {
Deals deals = new Deals();
return deals;
}
}
}
catch ( Exception e)
{
Log.i("okkk", "Exception 1 : " + e);
}
return PlaceholderFragment.newInstance(position + 1);
}
MapActivity.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="@layout/abc_action_menu_layout"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nearby Resturants"
android:id="@+id/btn_rest"
android:layout_gravity="bottom" />
答案 0 :(得分:0)
您可以参考下面的代码,希望它有所帮助,但如果您能提供代码则更好。
public class MapFragment extends BaseFragment implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Location mCurrentLocation;
private MapView mMapView;
private GoogleMap mGoogleMap;
private LocationManager locationManager;
private String provider;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_map, null);
mMapView = (MapView) view.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
Button nearBy = (Button)view.findViewById(R.id.nearby_button);
nearBy.setOnClickListener(new View.OnClickListener(){
// DO SOMETHING FOR NEARBY BUTTOM
}
return view;
}
@Override
public void onResume() {
mMapView.onResume();
super.onResume();
initMap();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
private void initMap() {
Log.d("MapFragment", "init map");
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setInfoWindowAdapter(new PopupAdapter(mContext, getLayoutInflater(null)));
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return true;
}
});
MapsInitializer.initialize(mContext);
}
});
}
private void locateMyLocation(Location currentLocation) {
mGoogleMap.addMarker(new MarkerOptions()
.title(mContext.getResources().getString(R.string.map_user_location_title))
.anchor(0.0f, 1.0f)
.position(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude())));
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
mGoogleMap.getUiSettings().setZoomControlsEnabled(false);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 15.0f));
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
locateMyLocation(mCurrentLocation);
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
protected synchronized void buildGoogleApiClient() {
Log.d("MapFragment", "buildGoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}