gpt4 book ai didi

android - PlacePicker 位置显示在 map 上

转载 作者:行者123 更新时间:2023-11-29 02:25:57 26 4
gpt4 key购买 nike

我使用地点选择器 API 获取了用户当前位置。现在我想在我的 Activity 中显示用户选择的位置。下面是我的代码:

public class StoreFinder extends AppCompatActivity implements OnMapReadyCallback {

Context context;
private final static int PLACE_PICKER_REQUEST = 999;
private GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
private boolean wifi, mobileData;
Marker mCurrLocationMarker;
String placeAddress, placeName;
LatLng toLatLng;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_finder);
context = StoreFinder.this;
wifi = Utils.WifiEnable(context);
mobileData = Utils.isMobileDataEnable(context);
/* declaration of map client */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(StoreFinder.this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
// open map

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_PICKER_REQUEST && resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
placeName = String.format("%s", place.getName());
String latitude = String.valueOf(place.getLatLng().latitude);
String longitude = String.valueOf(place.getLatLng().longitude);
placeAddress = String.format("%s", place.getAddress());
toLatLng = place.getLatLng();
Marker marker = mMap.addMarker(new
MarkerOptions().position(toLatLng)
.title(placeName).snippet(placeAddress)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(toLatLng,11));

它在地点选择器上运行良好。当我添加标记时, Activity 因空指针异常而崩溃。

我关注 place picker链接。

下面是错误

 Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference                                                                                      

最佳答案

您需要初始化 map 。假设您的 Activity 布局有一个 map fragment ,如下所示:

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

在您的应用的 onCreate 上,您需要:

// Obtain the SupportMapFragment and get notified when the map is ready to be used.
final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

并且您需要将 onMapReady 方法实现为:

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}

这样您就可以定义崩溃报告中为 NULL 的 mMap 对象。

关于android - PlacePicker 位置显示在 map 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202458/

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