我正在使用谷歌地图显示两个不同的位置。第一个是我使用geofire getlocation从firebase-databse获得的灶具位置。第二个位置是当前用户,我想从placepicker获得然后在地图上显示。
但不幸的是,从placepicker选择位置后,我只能在地图上获取电磁炉位置。如何在地图上显示这两个位置?
这是相关的代码。
private GoogleMap mMap;
SharedPreferences mPrefrences;
DatabaseReference locationRefrence = FirebaseDatabase.getInstance().getReference("Expert Location Information");
GeoFire geoFire = new GeoFire(locationRefrence);
Button btnConfirmLocation;
private int PLACE_PICKER_REQUEST=999;
LatLng userLocation;
String placeName,placeAddress;
Boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_for_cooker);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mPrefrences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor mEditor = mPrefrences.edit();
btnConfirmLocation = (Button)findViewById(R.id.btnConfirmLocation);
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
Intent intent = builder.build(MapsActivityForCooker.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PLACE_PICKER_REQUEST){
if (requestCode == RESULT_OK){
Place place = PlacePicker.getPlace(data,this);
placeName = String.format("Place: %s", place.getName());
placeAddress = String.format("Address: %s", place.getAddress());
userLocation = new LatLng(place.getLatLng().latitude,place.getLatLng().longitude);
mMap.addMarker(new MarkerOptions().position(userLocation)
.title(placeName).snippet(placeAddress)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,11));
flag = true;
}
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
final Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
Canvas canvas1 = new Canvas(bmp);
Paint color = new Paint();
canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.cooker), 0,0, color);
String location = mPrefrences.getString(getString(R.string.COOKER_ID)," ");
geoFire.getLocation(location, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
LatLng cookerLocation = new LatLng(location.latitude,location.longitude);
mMap.addMarker(new MarkerOptions().position(cookerLocation)
.title("Cooker Location")
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(cookerLocation));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15),2000,null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if(flag == true){
Toast.makeText(getApplicationContext(),"User location",Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions().position(userLocation)
.title(placeName).snippet(placeAddress)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,11));
}
btnConfirmLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MapsActivityForCooker.this,User_sign.class));
}
});
}