我正在尝试在地图上添加标记。但.search()
返回null。
可能是什么问题?
ActivityPlace.java
initializeMap()
activity_place.xml
public class ActivityPlace extends AppCompatActivity {
GoogleMap mMap;
LatLng latlng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place);
if (initializeMap() && checkServices()) {
latlng = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions().position(latlng);
Marker marker = mMap.addMarker(options);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latlng, 12);
mMap.animateCamera(update);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Launch Map Page
}
});
}
}
/*
Method to check Google services before initiating map
*/
public boolean checkServices() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS){
return true;
}else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
}else {
Toast.makeText(this, "Cant connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
/*
Initialize the map and get a handle to the map to set its location
*/
private boolean initializeMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
});
}
return (mMap != null);
}
}
activity_map_embedded_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/place_layout">
<TextView
style="@style/txtPrimaryHeading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="@+id/place_name"/>
<include
android:id="@+id/map"
layout="@layout/activity_map_embedded_fragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:1)
getMapAsync
是异步调用,它将在后台运行,您的方法将仅返回false值。
我的建议是在调用onMapReady
时执行其他操作。
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
latlng = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions().position(latlng);
Marker marker = mMap.addMarker(options);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latlng, 12);
mMap.animateCamera(update);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Launch Map Page
}
});
}
});
答案 1 :(得分:0)
在初始化地图之前调用它。
GoogleApiClient mGoogleApiClient ;
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
答案 2 :(得分:0)
试试这个。它确实有效。
GoogleApiClient mGoogleApiClient ;
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}