将当前位置添加到 fragment.class
Posted
技术标签:
【中文标题】将当前位置添加到 fragment.class【英文标题】:Add current location to a fragment.class 【发布时间】:2018-11-06 19:02:35 【问题描述】:我有点困惑...在互联网上找不到任何东西。
我想在标签片段中显示谷歌地图。它运作良好。但是如何设置片段的当前经度和纬度?
这里是xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".TabMap"
android:background="#DBDBDB">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_
android:layout_
android:layout_marginBottom="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp">
</com.google.android.gms.maps.MapView>
地图片段
public class TabMap extends Fragment implements OnMapReadyCallback
GoogleMap mGoogleMap ;
MapView mMapView;
View mView;
Location location = new Location();
public TabMap()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_tab_map, container, false);
return mView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
mMapView = (MapView) mView.findViewById(R.id.map);
if(mMapView != null)
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap)
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(new LatLng(51.1657, 10.4515)).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(51.1657, 10.4515)).zoom(6).bearing(0).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
location.class
public class Location extends AppCompatActivity
private static final int REQUEST_CODE = 1000;
TextView txtLocation;
Button btnLocation, btnStop;
AutoCompleteTextView gMap;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
LocationCallback locationCallback;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
case REQUEST_CODE:
if (grantResults.length > 0)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
else if (grantResults[0] == PackageManager.PERMISSION_DENIED)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
);
txtLocation = (TextView) findViewById(R.id.txtLocation);
btnLocation = (Button) findViewById(R.id.btnLocation);
btnStop = (Button) findViewById(R.id.btnStop);
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_CODE);
else
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
btnLocation.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(Location.this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_CODE);
return;
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
btnLocation.setEnabled(!btnLocation.isEnabled());
btnStop.setEnabled(!btnStop.isEnabled());
);
btnStop.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(Location.this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_CODE);
return;
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
btnLocation.setEnabled(!btnLocation.isEnabled());
btnStop.setEnabled(!btnStop.isEnabled());
);
public void buildLocationCallBack()
locationCallback = new LocationCallback()
@Override
public void onLocationResult(LocationResult locationResult)
for(android.location.Location location:locationResult.getLocations())
txtLocation.setText(String.valueOf(location.getLatitude())
+ "/" +
String.valueOf(location.getLongitude()));
;
private void buildLocationRequest()
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setSmallestDisplacement(10);
所以。我有一个导航栏,可以选择添加位置(location.class) 它打开并点击“当前位置”按钮,它在 TextView 中显示纬度和经度。
但是我如何将这些信息传递给 TabMap.class、googleMap.addMarker 方法呢?
提前致谢!
【问题讨论】:
在 onMapReady map.setMyLocationEnabled(true) 中添加这个; 【参考方案1】:在onMapReady
方法中替换这段代码
@Override
public void onMapReady(GoogleMap googleMap)
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener()
@Override
public void onMyLocationChange(Location location)
if(location!=null)
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
);
【讨论】:
【参考方案2】:你的问题是你不能让你的活动和片段通信。实现此目的的一种简单方法是使用库 EventBus : https://github.com/greenrobot/EventBus
请阅读文档。
【讨论】:
【参考方案3】:将此代码放入 OnMapReady 方法中:(它在蓝点中显示您的位置。)
@Override
public void onMapReady(GoogleMap googleMap)
if (mMap != null)
return;
mMap = googleMap;
if (mMap == null)
return;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
return;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(new mMap.OnMyLocationChangeListener()
@Override
public void onMyLocationChange(Location location)
if(location!=null)
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
);
【讨论】:
非常感谢!必须在“getActivity().getApplicationContext()”中更改“this”。现在它正在工作。太棒了! 如果您发现我的回答对使用upvote the answer.thanks 支持他人很有帮助以上是关于将当前位置添加到 fragment.class的主要内容,如果未能解决你的问题,请参考以下文章
ANDROID STUDIO - 如何将标记添加到当前位置?
使用当前位置将折线绘制到目的地,并在谷歌地图上添加标记,标记颤动
如何在 Google Maps v2 上将标记添加到当前位置?