The other day I asked, as fill from a cursor. I got on top of that focus all brands with the maximum zoom.
But now I have a problem and fill in the custom view, where there is title, description, and latitude and longitude.
It takes me head, I'm trying to look as filling sight, but each does so in a way. I am not able.
Here my code
info_windows_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Esto es un titulo"
android:id="#+id/tv_title"
android:textStyle="bold"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="tv_descripcion"
android:id="#+id/tv_descripcion"
android:textSize="16dp" />
<TextView
android:id="#+id/tv_lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lat" />
<TextView
android:id="#+id/tv_lng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lang" />
</LinearLayout>
CajerosMaps
public class CajerosMaps extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Cursor cursor;
MiCajeroOperacional mb;
LatLngBounds.Builder builder;
CameraUpdate cu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cajeros_maps);
mb = MiCajeroOperacional.getInstance(getApplicationContext());
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
cursor = mb.listar();
mMap = googleMap;
LatLng mark = null;
List<Marker> markersList = new ArrayList<Marker>();
int i = 0;
while (cursor.moveToNext()) {
double lat = cursor.getDouble(cursor.getColumnIndex("lat"));
double lng = cursor.getDouble(cursor.getColumnIndex("lng"));
mark = new LatLng(lat, lng);
Marker markCajero = mMap.addMarker(new MarkerOptions().position(mark).title("Sucursal Nº" + i).snippet(cursor.getString(cursor.getColumnIndex("direccion"))));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(mark), 15);
markersList.add(markCajero);
i++;
}
cursor.close();
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
int padding = 50;
LatLngBounds bounds = builder.build();
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.moveCamera(cu);
}
}
Related
This has to be the most absurd thing ever.
I have a RecyclerView with repeated custom items. Inside these items, there are a few textfields, buttons and a single MapView.
The issue is that when the list loads, the MapView only displays the Google logo and no other tile or detail (or marker). However, when I tap once on the map, it shows the marker I added. On the next tap, it loads a pixellated map. On another tap, it loads a better quality map. On further clicks it adds the text labels for nearby locations. LatLngBounds are also not working but that's a secondary problem.
Why is this happening?
My code is as follows:
JobAdapter.java
public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder>
{
private Context context;
private static List<Job> jobList;
private HashSet<MapView> mapViews = new HashSet<>();
private GoogleMap googleMap;
public JobAdapter(Context con, List<Job> jobs)
{
context = con;
jobList = jobs;
}
#Override
public int getItemCount()
{
return jobList.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.listitem_booking, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
mapViews.add(viewHolder.mapView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
Job job = jobList.get(position);
holder.mapView.setClickable(false);
if(job.getJobType().equalsIgnoreCase("Now"))
{
holder.pickup.setBackgroundColor(ContextCompat.getColor(context, R.color.lightRed));
}
holder.pickup.setText(job.getPickupAddress());
if(job.getDestinationAddress() != null && !job.getDestinationAddress().equalsIgnoreCase(""))
{
holder.destination.setText(job.getDestinationAddress());
}
else
{
holder.destination.setVisibility(View.GONE);
}
holder.person.setText(job.getContact());
holder.datetime.setText(job.getDate() + " at " + job.getTime());
}
class ViewHolder extends RecyclerView.ViewHolder implements /*View.OnClickListener,*/ OnMapReadyCallback
{
#BindView(R.id.pickup)
TextView pickup;
#BindView(R.id.destination)
TextView destination;
#BindView(R.id.person)
TextView person;
#BindView(R.id.datetime)
TextView datetime;
#BindView(R.id.map_listitem)
MapView mapView;
#BindView(R.id.acceptJob)
Button acceptJob;
#BindView(R.id.declineJob)
Button declineJob;
#BindView(R.id.buttonLayout)
LinearLayout buttonLayout;
private ViewHolder(View itemView)
{
super(itemView);
ButterKnife.bind(this, itemView);
// itemView.setOnClickListener(this);
mapView.onCreate(null);
mapView.getMapAsync(this);
}
private void addMarkers(Job job)
{
googleMap.clear();
boolean hasDestination = true;
String[] destinationLatlng = null;
LatLng destination = null;
if(job.getDestinationAddress() == null || job.getDestinationAddress().equalsIgnoreCase(""))
{
hasDestination = false;
}
else
{
destinationLatlng = job.getDestinationLatLong().split(",");
destination = new LatLng(Double.valueOf(destinationLatlng[0]), Double.parseDouble(destinationLatlng[1]));
}
final String[] pickupLatlng = job.getPickupLatLong().split(",");
final LatLng pickup = new LatLng(Double.valueOf(pickupLatlng[0]), Double.parseDouble(pickupLatlng[1]));
if(hasDestination)
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
googleMap.addMarker(new MarkerOptions()
.position(destination)
.title(job.getDestinationAddress()));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pickup);
builder.include(destination);
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 5);
googleMap.animateCamera(cameraUpdate);
}
else
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(pickup, 15);
googleMap.animateCamera(cameraUpdate);
}
}
/*#Override
public void onClick(View view)
{
final Job job = jobList.get(getAdapterPosition());
}*/
#Override
public void onMapReady(GoogleMap gMap)
{
googleMap = gMap;
addMarkers(jobList.get(getAdapterPosition()));
}
}
}
listitem_booking
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
app:cardElevation="2dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/pickup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textSize="16sp"
android:text="Complex"
android:background="#color/lessLightGreen"
android:gravity="center_vertical"
android:drawableStart="#drawable/google_maps"
android:drawablePadding="2dp"
android:textColor="#color/colorPrimaryText"/>
<TextView
android:id="#+id/destination"
android:layout_below="#id/pickup"
android:text="Golra"
android:visibility="visible"
android:drawableStart="#drawable/directions"
android:drawablePadding="2dp"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/person"
android:drawablePadding="2dp"
android:layout_below="#id/destination"
android:text="Asfandyar Khan"
android:drawableStart="#drawable/account"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/datetime"
android:layout_below="#id/person"
android:text="7th April 2017 at 9:00am"
android:drawableStart="#drawable/time"
style="#style/listitem_secondary_text"/>
<com.google.android.gms.maps.MapView
android:id="#+id/map_listitem"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginTop="2dp"
android:layout_below="#id/datetime"
map:liteMode="true"
android:padding="10dp"/>
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/map_listitem"
android:gravity="end"
android:layout_margin="4dp">
<Button
android:backgroundTint="#color/colorPrimary"
android:textColor="#android:color/white"
android:id="#+id/acceptJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept"/>
<Button
android:backgroundTint="#color/darkRed"
android:textColor="#android:color/white"
android:id="#+id/declineJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decline"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
I've tried various things but nothing seems to be working.
Try adding onResume, like this:
mapView.onCreate(null);
mapView.getMapAsync(this);
mapView.onResume();
Edit:
I've just noticed you are using
map:liteMode="true"
Try removing it (at least to test) or adding the following to the xml:
map:cameraZoom="15"
map:mapType="normal"
Either way, I think the onResume is needed.
When I use liteMode it sometimes takes a few seconds (about 5) for the map to show after the logo.
There might another issue in your code. The "map" should be:
xmlns:map="http://schemas.android.com/apk/res-auto"
but not:
xmlns:map="http://schemas.android.com/tools"
I'm facing the problem about get detail information(data) from marker to NestedScrollView of BottomSheet.
When I clicked marker, NestedScrollView will scroll up from bottom and display corresponding data. Im using json to get data.
My source code:
public class SeekingMapActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seeking_main);
...
ArrayList<HashMap<String, String>> location = null;
String url = "myURL";
try {
JSONArray data = new JSONArray(getHttpGet(url));
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("title", c.getString("title"));
map.put("avatar", c.getString("avatar"));
map.put("lat", c.getString("lat"));
map.put("mapLong", c.getString("mapLong"));
map.put("address", c.getString("address"));
location.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
// googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
IconGenerator tc = new IconGenerator(this);
String price = "1200K";
Bitmap bmp = tc.makeIcon(price); // pass the text you want.
lat = Double.parseDouble(location.get(0).get("lat").toString());
mapLong = Double.parseDouble(location.get(0).get("mapLong").toString());
LatLng coordinate = new LatLng(lat, mapLong);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setMapToolbarEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(true); // Enable RotateGestures
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 15));
for (int i = 0; i < location.size(); i++) {
lat = Double.parseDouble(location.get(i).get("lat").toString());
mapLong = Double.parseDouble(location.get(i).get("mapLong").toString());
String title = location.get(i).get("title").toString();
String avatar = location.get(i).get("avatar".toString());
String address = location.get(i).get("address").toString();
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, mapLong))
.title(title)
.snippet(address)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)); // .anchor(0.5f, 0.6f)
googleMap.addMarker(marker);
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(isClick==false)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
else
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
isClick=!isClick;
// Toast.makeText(SeekingMapActivity.this, arg0.getTitle(), Toast.LENGTH_SHORT).show();// display toast
return true;
}
});
}
}
...
}
Layout of BottomSheet:
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="270dp"
android:clipToPadding="true"
android:background="#color/white"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
> <!-- android:background="#android:color/background_holo_light" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/mercedes"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1,200,000"
android:textSize="20dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Số cửa: 4 cửa"
android:textSize="14dp"/>
<!-- android:textAppearance="?android:attr/textAppearanceMedium" -->
<TextView
android:id="#+id/textView1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Số ghế: 4 chỗ"
android:textSize="14dp"/>
</LinearLayout>
<TextView
android:id="#+id/textView1d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Địa chỉ: Quân Cầu giấy, Hà Nội"
android:textSize="14dp" />
<TextView
android:id="#+id/textView1e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KIỂM TRA TÌNH TRẠNG XE"
android:textStyle="bold"
android:textColor="#color/greenColor"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
I don't know how to get data from maker to NestedScrollView
Example:
When click: Marker A, data of marker A will display on NestedScrollView
Marker B, data of marker B will display on NestedScrollView
If you need more information, I will post more!
1 - Create model class with your attributes like title, avatar, address etc...
2 - Create an Arraylist of your model type
3 - Add all those data in your arraylist
4 - Now in onMarkerClick method of your marker, iterate for loop and compare title or unique id of that marker with position of your arrayList title/unique id like below:
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
for (int i = 0; i < arrayList.size(); i++) {
if (marker.getTitle().equals(finalMSearchResultModel.getRecords().get(i).getHotelName())) {
// YOUR ACTION GOES HERE
}
}
return false;
}
});
There are 10 Geo locations(longitude&latitude),Markers should be pointed based on the distance between my current location to the first nearest Geo location, From that Geo location to the next nearby location and vise versa.For this particular scenario will any Google map API support or shall we need write own algorithm. If any please let me know.
Thanks in Advance
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#android:color/white">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:tint="#android:color/white"
android:backgroundTint="#1baec3"
app:fabSize="auto"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="33.8dp"
android:layout_marginRight="13.5dp"
app:srcCompat="#drawable/search" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/padding_margin_20"/>
<RelativeLayout
android:id="#+id/rl_top"
android:layout_width="match_parent"
android:layout_height="69dp"
android:layout_alignParentTop="true"
android:background="#drawable/gradient_gray" >
<ImageView
android:id="#+id/img_toggle"
android:layout_width="8.5dp"
android:layout_height="8.5dp"
android:layout_centerVertical="true"
android:src="#drawable/icon_toggle"
android:tint="#color/text_color"
android:scaleType="fitXY" />
<TextView
android:id="#+id/tv_search_directory_near"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img_toggle"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/padding_margin_5"
android:text="#string/near"
android:textSize="9sp"
android:textColor="#797979"/>
<LinearLayout
android:id="#+id/ly_map_radius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11.5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/tv_search_directory_near"
android:background="#drawable/bg_radius_black_line"
android:orientation="horizontal">
<ImageView
android:layout_width="10.3dp"
android:layout_height="6dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="9.5dp"
android:layout_marginTop="9.3dp"
android:layout_marginBottom="9.8dp"
android:src="#drawable/down_arrow"
android:tint="#797979"/>
<TextView
android:id="#+id/tv_map_radius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6.3dp"
android:layout_marginTop="9.3dp"
android:layout_marginBottom="8.8dp"
android:layout_marginRight="11dp"
android:text="#string/spinner_radius"
android:textSize="9sp"
android:fontFamily="#font/roboto"
android:textColor="#797979"/>
</LinearLayout>
<LinearLayout
android:id="#+id/ly_map_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10.8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ly_map_radius"
android:background="#drawable/bg_radius_black_line"
android:orientation="horizontal">
<ImageView
android:layout_width="10.3dp"
android:layout_height="6dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="9.5dp"
android:layout_marginTop="9.3dp"
android:layout_marginBottom="9.8dp"
android:src="#drawable/down_arrow"
android:tint="#797979"/>
<TextView
android:id="#+id/tv_map_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6.3dp"
android:layout_marginTop="9.5dp"
android:layout_marginBottom="#dimen/padding_margin_7"
android:layout_marginRight="#dimen/padding_margin_15"
android:text="#string/spinner_country"
android:textSize="9sp"
android:textColor="#1baec3"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
public class Map_Fragment extends Fragment implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback, View.OnClickListener{
private GoogleMap mMap;
LatLng markerLocation;
Projection projection;
Point screenPosition;
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
private Marker mPerth;
private Marker mSydney;
private Marker mBrisbane;
LinearLayout ly_map_radius, ly_map_country;
TextView tv_map_radius, tv_map_country;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ly_map_radius = root.findViewById(R.id.ly_map_radius);
ly_map_country = root.findViewById(R.id.ly_map_country);
tv_map_radius = root.findViewById(R.id.tv_map_radius);
tv_map_country = root.findViewById(R.id.tv_map_country);
return root;
}
#Override
public void onViewCreated(final View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ly_map_radius.setOnClickListener(this);
ly_map_country.setOnClickListener(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
projection = mMap.getProjection();
LatLng sydney = new LatLng(-34, 151);
mPerth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.title("Perth")
.snippet("Population: Perth")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
mSydney = mMap.addMarker(new MarkerOptions()
.position(SYDNEY)
.title("Sydney")
.snippet("Population: Sydney")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
mSydney.showInfoWindow();
mBrisbane = mMap.addMarker(new MarkerOptions()
.position(BRISBANE)
.title("Brisbane")
.snippet("Population: Brisbane")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
mBrisbane.showInfoWindow();
mMap.setOnMarkerClickListener(this);
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
switch (v.getId()){
case R.id.ly_map_country:
builder.setTitle("Select Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tv_map_country.setText(countries[which]);
}
});
break;
case R.id.ly_map_radius:
builder.setTitle("Select Radius");
builder.setItems(radius, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tv_map_radius.setText(radius[which] + " km radius");
}
});
break;
}
AlertDialog dialog = builder.create();
dialog.show();
}
}
I have a dialog fragment where i want to show my map and mark the lat and log which i have with with me.
But i am getting my getMap() as NULL. This is what i did
public class EventDetailsDialogFragment extends DialogFragment{
TextView title, desc, sdate, edate, room, loctn;
FeedObjModel selectedFeedObject;
Date netDate;
SimpleDateFormat sdf;
GoogleMap theMap;
MarkerOptions markerOptions;
FragmentActivity myContext;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.event_details_dialog_layout, container);
title = (TextView) view.findViewById(R.id.title_event_details_TV);
desc = (TextView) view.findViewById(R.id.description_event_details_TV);
sdate = (TextView) view.findViewById(R.id.start_date_event_details_TV);
edate = (TextView) view.findViewById(R.id.end_date_event_details_TV);
loctn = (TextView) view.findViewById(R.id.locEdTV);
long timestamp1 = Long.parseLong(selectedFeedObject.eventstartDate);
long timestamp2 = Long.parseLong(selectedFeedObject.eventendDate);
try{
sdf = new SimpleDateFormat("MMM dd, yyyy");
netDate = (new Date(timestamp1*1000));
sdate.setText(sdf.format(netDate));
netDate = (new Date(timestamp2*1000));
edate.setText(sdf.format(netDate));
} catch(Exception ex){
}
getDialog().setTitle(""+selectedFeedObject.subject);
title.setText(selectedFeedObject.subject);
desc.setText(selectedFeedObject.eventDescription);
loctn.setText(selectedFeedObject.eventLocation);
/* theMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.mapED))
.getMap();*/
SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapED, fragment).commit();
theMap = fragment.getMap();
Log.v("theMap1", ""+theMap);
Double lng = Double.parseDouble(selectedFeedObject.eventlongitude);
Double lat = Double.parseDouble(selectedFeedObject.eventlatitude);
LatLng latLng = new LatLng(lng, lat);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(selectedFeedObject.eventLocation);
Log.v("markerOptions", ""+markerOptions);
theMap.addMarker(markerOptions);
theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate center = CameraUpdateFactory
.newLatLng(new LatLng(lat, lng));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
theMap.moveCamera(center);
theMap.animateCamera(zoom);
return view;
}
public void setEventDetails(FeedObjModel _selectedFeedObject) {
// TODO Auto-generated method stub
selectedFeedObject = _selectedFeedObject;
}
}
i am getting 'themap' as NULL
.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/title_event_details_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/description_event_details_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Date and Time"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/start_date_event_details_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="End Date and Time"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/end_date_event_details_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Location"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/locEdTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<FrameLayout
android:id="#+id/mapED"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</LinearLayout>
I wrote the following code in onResume() now this is working...i don't know is this the right way.
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
theMap = mapView.getMap();
Log.v("theMap1", ""+theMap);
Log.v("lng", ""+lng);
Log.v("lat", ""+lat);
latLng = new LatLng(lng, lat);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(locString);
Log.v("markerOptions", ""+markerOptions);
theMap.addMarker(markerOptions);
theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate center=
CameraUpdateFactory.newLatLngZoom(new LatLng(lat,
lng),16);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
theMap.moveCamera(center);
theMap.animateCamera(zoom);
}
in oncreateview
mapView = CustomMapFragmentForEventDetails.newInstance();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.mapED, mapView);
fragmentTransaction.commit();
lng = Double.parseDouble(selectedFeedObject.eventlongitude);
lat = Double.parseDouble(selectedFeedObject.eventlatitude);
and in CustomMapFragmentForEventDetails
public class CustomMapFragmentForEventDetails extends SupportMapFragment {
SupportMapFragment mSupportMapFragment;
GoogleMap googleMap;
OnMapReadyListener mOnMapReadyListener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
Log.v("Inside CustomMapFrag", "Success");
View root = inflater.inflate(R.layout.event_details_dialog_layout, null, false);
initilizeMap();
return root;
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
mOnMapReadyListener = (OnMapReadyListener) activity;
}
private void initilizeMap()
{
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapED);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapED, mSupportMapFragment).commit();
}
if (mSupportMapFragment != null)
{
googleMap = mSupportMapFragment.getMap();
if (googleMap != null)
{
Log.v("MAP GOOGLE in CustomMApfragment", ":: "+googleMap);
mOnMapReadyListener.onMapReady(googleMap);
}
}
}
public static interface OnMapReadyListener {
void onMapReady(GoogleMap googleMap);
}
}
I guess this is because even thought the Transaction of the map fragment is committed it does not execute immediately. You have to use : FragmentManager.executePendingTransactions() this way :
SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapED, fragment).commit();
getFragmentManager().executePendingTransactions();
theMap = fragment.getMap();
In my android app, I have a google maps v2 inside a fragment with marker of places. When I touch in a marker, it displays a RelativeLayout with the name of the marker. However, I would like that when I touch anywhere in the map, this RelativeLayout is hidden.
My code is this:
fragment_mapa.xml
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:gravity="center" />
<RelativeLayout
android:id="#+id/sliding_up"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#fff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:clickable="true"
android:focusable="false"
android:animateLayoutChanges="true"
android:visibility="invisible" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="10dp"/>
</RelativeLayout>
Code where it creates the markers and onClick method to display the RelativeLayout
public void addItemsToMap() {
appState.mapa.clear();
if (appState.lista.isEmpty()) {
appState.readPlaces(5000, 0, appState.idCategoria);
}
appState.mapa.setOnMarkerClickListener(this);
appState.mapa.setOnInfoWindowClickListener(getInfoWindowClickListener());
LatLng miPosicion = new LatLng(obtLatitud, obtLongitud);
appState.mapa.addMarker(new MarkerOptions()
.position(miPosicion)
.title("Mi posición")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
for (int i = 0; i < appState.lista.size(); i++) {
LatLng posItem = new LatLng(appState.lista.get(i).latitud,appState.lista.get(i).longitud);
appState.mapa.addMarker(new MarkerOptions()
.position(posItem)
.title(appState.lista.get(i).nombre)
.snippet(appState.lista.get(i).descripcion)
/*.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))*/);
Log.v("MAPA", "Marker " + i + ": " + appState.lista.get(i).nombre);
}
}
#Override
public boolean onMarkerClick(final Marker marker) {
if(marker != null) {
//marker.showInfoWindow();
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.VISIBLE);
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
slideLayout.startAnimation(slide);
TextView t;
t = (TextView) findViewById(R.id.name);
t.setText(marker.getTitle());
return true;
} else {
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.INVISIBLE);
return false;
}
}
// try this :
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng arg0) {
}
});