我正在开发一个简单的地图应用程序,我使用navigationDrawer
将我的地图放在drawer
的第一个选项中..现在问题是当我把任何按钮放在同一个地方时fragment
地图或相同的布局,它不执行我想要的!
例如:当我点击button
时,我需要放大我的位置。
我尝试了一些我在网站上找到的解决方案,但没有运气..
xml代码:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_height="match_parent"
android:layout_width="405dp" android:id="@+id/mapfragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
/>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="onClick"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java代码:
public class maptest extends Fragment implements OnMapReadyCallback,View.OnClickListener {
GoogleMap mp;
@Nullable
View v1;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.newmapfragment, container,false);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mp) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
@Override
public void onClick(View view) {
Button b = (Button) view.findViewById(R.id.bt1);
b.setOnClickListener(this);
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
}
任何帮助将不胜感激..
答案 0 :(得分:1)
您可能会忘记片段的onCreateView:
vi=inflater.inflate(your_fragment_layout_xml,container,false);
答案 1 :(得分:1)
试试这个你忘了在 v1
onCreateView
充气
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v1=inflater.inflate(your_fragment_layout_xml,container,false);
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
}
});
return v1;
}
修改-1 强>
在Fragment
Button b = (Button) v1.findViewById(R.id.bt1);
b.setOnClickListener(this);
修改-2 强>
从android:onClick="onClick"
Button
移除 XML
答案 2 :(得分:1)
用以下代码替换maptest片段
public class maptest extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
GoogleMap mMap;
double latitude;
double longitude;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
View v1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v1 = inflater.inflate(R.layout.fragment_map, container, false);
((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
//Check if Google Play Services Available or not
if (!CheckGooglePlayServices()) {
Log.d("onCreate", "Finishing test case since Google Play Services are not available");
} else {
Log.d("onCreate", "Google Play Services available.");
}
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mLastLocation!=null && mMap!=null)
{
/////to change map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//////to zoom on current location
LatLng sydney = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(sydney)
.title("current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
});
return v1;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
}
private boolean CheckGooglePlayServices() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(context);
if (result != ConnectionResult.SUCCESS) {
if (googleAPI.isUserResolvableError(result)) {
googleAPI.getErrorDialog((Activity) context, result,
0).show();
}
return false;
}
return true;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
Log.d("onLocationChanged", "entered");
mLastLocation = location;
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Log.d("onLocationChanged", "Removing Location Updates");
}
Log.d("onLocationChanged", "Exit");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Initialize Google Play Services
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
@Override
public void onResume() {
super.onResume();
}
}
答案 3 :(得分:0)
首先删除任何一个onclick方法,因为现在你正在使用两个onclick方法。你应该添加这样的按钮: -
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</RelativeLayout>
View v1;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
v1 = inflater.inflate(R.layout.newmapfragment, container, false);
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
}
});
return v1;
}
点击here!获取当前位置。