gpt4 book ai didi

android - 如何在Android中获取当前位置的经纬度?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:31 25 4
gpt4 key购买 nike

我有一个 fragment ,我想在其中显示用户(设备)的当前位置。我使用谷歌地图 Android API v2。如何获取设备当前的经纬度?

在下面您可以看到我使用的代码。不幸的是,应用程序运行不正常,与我预期的不同。它只显示 map ,但没有我设置的标记,也没有当前位置。任何人都可以检查并修复它吗?怎么了?感谢您的帮助!

编辑:

MainFragment.java

public class MainFragment extends Fragment{

private SupportMapFragment mSupportMapFragment;
private GoogleMap mGoogleMap;

public BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

Bundle extras = intent.getExtras();
LatLng temp = new LatLng(extras.getDouble("Latitude"),extras.getDouble("Longitude"));
}
};

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

FragmentManager mFragmentManager = getChildFragmentManager();
mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentById(R.id.map_container);
if (mSupportMapFragment == null) {
mSupportMapFragment = SupportMapFragment.newInstance();
mFragmentManager.beginTransaction().replace(R.id.map_container, mSupportMapFragment).commit();
}

//Broadcast/ Filter
IntentFilter filter = new IntentFilter();
filter.addAction(LocationService.BROADCAST_ACTION);
getActivity().registerReceiver(receiver, filter);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//Start service
getActivity().startService(new Intent(getActivity(),LocationService.class));
}

@Override
public void onResume(){
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

Log.d(TAG, "BroadcastReceiver works!");

Bundle extras = intent.getExtras();
final LatLng temp = new LatLng(extras.getDouble("Latitude"),extras.getDouble("Longitude"));

if (mGoogleMap != null) {
mGoogleMap = mSupportMapFragment.getMap();
mGoogleMap.addMarker(new MarkerOptions().position(temp).title("Marker").snippet("Location"));
}
try {
CameraUpdate center = CameraUpdateFactory.newLatLng(temp);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15f);
mGoogleMap.moveCamera(center);
mGoogleMap.animateCamera(zoom);
}catch (NumberFormatException e) {
//Some code here for exception
}
}
};
}

@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

fragment_main.xml

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_container">

<!-- The map fragments will go here -->
</RelativeLayout>

最佳答案

此信息以及更多信息在您的应用可以从融合位置提供程序检索的 Location 对象中可用:https://developer.android.com/training/location/retrieve-current.html .

关于android - 如何在Android中获取当前位置的经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28397402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com