I am trying to add some image button in my marker info.
Though the buttons are added in the view but i cat not click on them.
When I try to click it clicks on the whole view.
My code us given below.
MainActivity.java
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout base;
base = (RelativeLayout) findViewById(R.id.base);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
try {
initilizeMap(); // Loading map
} catch (Exception e) {
Log.d("asdfadfadf", "asdfasdf");
}
googleMap.setMyLocationEnabled(true);
//googleMap.clear();
IconGenerator i = new IconGenerator(getBaseContext());
i.setStyle(IconGenerator.STYLE_BLUE);
Bitmap tt = i.makeIcon("asdf");
LatLng myLocation = new LatLng(22.899171, 89.500186);
Marker myLocMarker = googleMap.addMarker(new MarkerOptions()
.position(myLocation)
.icon(BitmapDescriptorFactory.fromBitmap(tt))
.title("sssss"));
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker mk) {
// TODO Auto-generated method stub
return null;
}
#Override
public View getInfoContents(Marker arg0) {
// TODO Auto-generated method stub
ImageButton set,from,favor;
View view = getLayoutInflater().inflate(R.layout.marker_info,null);
set = (ImageButton) view.findViewById(R.id.imageButtonDestination);
from = (ImageButton) view.findViewById(R.id.imageButtonFrom);
favor = (ImageButton) view.findViewById(R.id.imageButtonFavourite);
set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("sssssssssssssssss", "sssssssssssssssss");
}
});
return view;
}
});
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap(); // check if map is created successfully or not
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
don't know what to do.
Thanks in advance.
You can't add click listeners to the Marker's InfoWindow as it's not a real view. But a Bitmap that gets rendered from you layout.
From Google Docs: https://developers.google.com/maps/documentation/android/marker#info_windows
Note: The info window that is drawn is not a live view. The view is
rendered as an image (using View.draw(Canvas)) at the time it is
returned. This means that any subsequent changes to the view will not
be reflected by the info window on the map. To update the info window
later (e.g., after an image has loaded), call showInfoWindow().
Furthermore, the info window will not respect any of the interactivity
typical for a normal view such as touch or gesture events. However you
can listen to a generic click event on the whole info window as
described in the section below.
Set GoogleMap.OnInfoWindowClickListener() to your map, implement the interface. You get the marker passed to you, call you action form there:
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override public void onInfoWindowClick(Marker marker) {
//do your thing here
}
});
I don't understand very well what you require, but according understanding is something of a click on an image to add to the map (a marker). if so may try this:
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
//Your code when you do click
return false;
}
});
I hope help you, and Sorry for my english :(
Related
i want to show the location indicator when the map is loaded and add a marker whenever the map is clicked but none of these seem to work !
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
assert mapFragment != null;
mapFragment.getMapAsync(new OnMapInitListener() {
#Override
public void onMapReady(MapView mapView) {
OnlineManager.getInstance().enableOnlineMapStreaming(true);
PositionManager.getInstance().startPositionUpdating();
PositionManager.getInstance().enableRemotePositioningService();
mpView=mapView;
mpView.addMapGestureListener(new MapGestureAdapter() {
#Override
public boolean onMapClicked(final MotionEvent e, final boolean isTwoFingers) {
MapMarker marker = new MapMarker(new GeoCoordinates(PositionManager.getInstance().getLastKnownPosition().getLongitudeAccuracy(),PositionManager.getInstance().getLastKnownPosition().getLatitudeAccuracy()));
mpView.addMapObject(marker);
return true;
}
});
}
#Override
public void onMapError(int error, String info) {}
});
}
You’re trying to create new Marker with getLongitudeAccuracy() and getLatitudeAccuracy(). You need to use geo coordinates!
If you want to add the marker to the position of last known gps signal you can use this code:
MapMarker marker = new MapMarker(PositionManager.getInstance().getLastKnownPosition().getCoordinates())
But as there can be no known position at that time it can result in no marker added. So be sure you have location turned on and strong signal. Based on your example it would make more sense to add marker to the position you clicked on. For that purpose use this code:
mpView.addMapGestureListener(new MapGestureAdapter() {
#Override
public boolean onMapClicked(final MotionEvent e, final boolean isTwoFingers) {
MapMarker marker = new MapMarker(mpView.geoCoordinatesFromPoint(e.getX(), e.getY()));
mpView.addMapObject(marker);
return true;
}
});
What I am trying to accomplish here is be able to show a AlertDialog when I click on a marker that is dynamically added on load (call an API, get positions and show them on map). This is done in the fragments onCreateView.
The map loads here:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, mapFragment).commit();
}
}
Later on, I switch from a SupportMapFragment to a GoogleMap:
#Override
public void onResume() {
super.onResume();
if (mMap == null && mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = Tools.configBasicGoogleMap(googleMap);
mMap.setMapType(sharedPref.getMapType());
}
});
}
}
So, logically, my Map should be ready as mMap. Now, I want to show a AlertDialog, so I do implements GoogleMap.OnMarkerClickListener in the class definition for the fragment and implement it here:
#Override
public boolean onMarkerClick(final Marker marker) {
Toast.makeText(getContext(), "Hello world", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Confirmation");
builder.setMessage("Pour confirmer le rapport, appuyez sur + 1.\nSi le rapport est faux, cliquez sur - 1.");
builder.setPositiveButton("+ 1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
URL link = null;
try {
link = new URL(S.base_url + "report/increment/" + marker.getTag().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
link.openStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
});
AlertDialog ab = builder.create();
ab.show();
return false;
}
But not even the Toast doesn't show up... When I click on a marker I get the Directions and Open in Google Maps buttons, and the map centers itself on the marker, but my method doesn't start.
To conclude, logically when I click one of these markers that I load, the method onMarkerClick should trigger itself, but this never happens! Any idea why? Thanks.
I don't know why but Google Maps API can't used as implement to fragments. Following code may work it for you. You just need to assign markerClickListener for local, not implementing MarkerClickListener.
//Detect location and set on map
mMapView.getMapAsync(new OnMapReadyCallback() {
#SuppressLint("MissingPermission")
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// Bla
// Bla
// Bla your codes about map
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
// Triggered when user click any marker on the map
return false;
}
});
}
});
I have code in which I want to update the textview and imageview of the InfoWindow. The image and text are retrieved from a sqlite database so I put this in an AsyncTask. When I want to update the textview and imageview of the InfoWindow from OnPostExecute, this doesn't work, the infowindow remains empty.
I found similar questions on StackOverflow but none of the answers solved my problem.
This is my code:
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
LatLng latLng = marker.getPosition();
// find location id in database
Location location = dbhandler.getLocationByLatLng(latLng);
final int id = location.getId();
addButton.setVisibility(View.VISIBLE);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// open load image fragment
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LoadImageFragment fragment = new LoadImageFragment();
// pass id to new fragment
Bundle bundle = new Bundle();
bundle.putInt("id", id);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
});
removeButton.setVisibility(View.VISIBLE);
removeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// remove markers and images
}
});
new AsyncTask<LatLng, Void, Location>() {
#Override
protected Location doInBackground(LatLng... params) {
LatLng latLng = params[0];
Location location = dbhandler.getLocationByLatLng(latLng);
return location;
}
// find image and text associated with Location
protected void onPostExecute(Location location) {
new AsyncTask<Location, Void, Image>() {
#Override
protected Image doInBackground(Location... params) {
Location location = params[0];
try {
image = dbhandler.getImageByLocationId(location.getId());
}
catch (Exception ex){
Log.d("debug", "failed to fetch image");
image = null;
}
return image;
}
#Override
protected void onPostExecute(Image image) {
// set image and description
if(image != null) {
infoImageView.setImageBitmap(image.getBitmap());
infoTextView.setText(image.getDescription());
updateInfoWindow(image);
}
}
}.execute(location);
}
}.execute(marker.getPosition());
marker.showInfoWindow();
return true;
}
});
// find Location in database
// Setting a custom info window adapter for the google map
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
View v = getActivity().getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
final LatLng latLng = arg0.getPosition();
infoImageView = (ImageView) v.findViewById(R.id.infoImage);
infoTextView = (TextView) v.findViewById(R.id.infoText);
if(image != null) {
infoImageView.setImageBitmap(image.getBitmap());
infoTextView.setText(image.getDescription());
}
return v;
}
});
}
});
The Info Windows documentation includes this note:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
So you have to hold a reference to the Marker whose info you're trying to show, download the image, set the image to your ImageView,and then re-call marker.showInfoWindow() at the end.
please declare your variable Globally
Image image;
I want to display like this.
https://lh6.ggpht.com/DOvS0NiHGyGC9Vn7JvkwznRBZ7I65j3zwpNAqLkP5y083ju7JQbwWi4NAXQLp5Wsavmn=h900
When google map marker gets tap, text will display below. It can be drag up and down. I don't get how it has been done.
public class MapsActivity extends FragmentActivity
{
Googlemap gm;
gm = ((SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.map))).getMap();
gm.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gm.setOnInfoWindowClickListener(this);
#Override
public void onInfoWindowClick(Marker marker) {
String a=marker.getTitle();
// how to display this title in same screen below with dragging up and down when map marker been tap
}
}
Please help me to solve this issue.Thanks in advance.
implement this method instead of infoWindowClick
gm.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
String title = "Any Thing You want";
marker.setTitle(title);
textView.setText("title");
return true;
}
});
You have to create hidden layout for it. Create one layout as per your need and do it GONE by default. But when you click like below and inflate that layout in you onClick method:
gm.setOnInfoWindowClickListener(this);
#Override
public void onInfoWindowClick(Marker marker) {
String a=marker.getTitle();
LayoutInflater inflater;
inflater = LayoutInflater.from(context);
View view;
view = adapterInflater.inflate(R.layout.adapter_add_event, null);
// how to display this title in same screen below with dragging up and down when map marker been tap
}
And visible this layout, do findViewById and make it visible.
or.. If your marker is draggable, use the following snippet
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragEnd(Marker marker) {
marker.setSnippet("Your Text");
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
}
});
I want to show an InfoWindow on markers in a Maps V2 fragment.
Thing is, I want to show BitMaps that are dynamically loaded from the web with Universal Image Downloader.
This is my InfoWindowAdapter:
class MyInfoWindowAdapter implements InfoWindowAdapter {
private final View v;
MyInfoWindowAdapter() {
v = getLayoutInflater().inflate(R.layout.infowindow_map,
null);
}
#Override
public View getInfoContents(Marker marker) {
Item i = items.get(marker.getId());
TextView tv1 = (TextView) v.findViewById(R.id.textView1);
ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
tv1.setText(i.getTitle());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.delayBeforeLoading(5000).build();
imageLoader.getMemoryCache();
imageLoader.displayImage(i.getThumbnailUrl(), iv, options,
new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
// TODO Auto-generated method stub
}
#Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
// TODO Auto-generated method stub
}
#Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
Log.d("MAP", "Image loaded " + imageUri);
}
#Override
public void onLoadingCancelled(String imageUri,
View view) {
// TODO Auto-generated method stub
}
});
return v;
}
#Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}
I have 2 problems with this:
As we know the InfoWindow is drawn and later changes to it (in my case the new BitMap on the ImageView) are not shown/ the InfoWindow is not being updated. How can I "notify" the InfoWindow to reload itself when the imageLoader has finished? When I put
marker.showInfoWindow()
into onLoadingComplete it created an infinite loop where the marker will pop up, start loading the image, pop itself up etc.
My second problem is that on a slow network connection (simulated with the 5000ms delay in the code), the ImageView in the InfoWindow will always display the last loaded image, no matter if that image belongs to that ImageWindow/ Marker.
Any suggestions on how to propperly implement this?
You should be doing Marker.showInfoWindow() on marker that is currently showing info window when you receive model update.
So you need to do 3 things:
create model and not put all the downloading into InfoWindowAdapter
save reference to Marker (call it markerShowingInfoWindow)
from getInfoContents(Marker marker)
when model notifies you of download complete call
if (markerShowingInfoWindow != null && markerShowingInfoWindow.isInfoWindowShown()) {
markerShowingInfoWindow.showInfoWindow();
}
I did something similar.
This was still giving me the recession error
if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) {
markerShowingInfoWindow.showInfoWindow();
}
So what i did was simply closes the window and open it again
if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) {
markerShowingInfoWindow.hideInfoWindow();
markerShowingInfoWindow.showInfoWindow();
}
for a better detail version of the same answer here is my original soultion LINK
I was also faced same situation and solved using the following code.
In my adapter I have added public variable
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
public String ShopName="";
-------
-------
#Override
public View getInfoWindow(Marker arg0) {
View v;
v = mInflater.inflate(R.layout.info_window, null);
TextView shop= (TextView) v.findViewById(R.id.tv_shop);
shop.setText(ShopName);
}
}
and added MarkerClickListener in my main activity
----
MarkerInfoWindowAdapter mMarkerInfoWindowAdapter;
----
----
#Override
public void onMapReady(GoogleMap googleMap) {
mMarkerInfoWindowAdapter = new MarkerInfoWindowAdapter(getApplicationContext());
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker arg0) {
mMarkerInfoWindowAdapter.ShopName= "my dynamic text";
arg0.showInfoWindow();
return true;
}
}
mMap.setInfoWindowAdapter(mMarkerInfoWindowAdapter);
}
I've used the code in this article, and it worked well.
http://androidfreakers.blogspot.de/2013/08/display-custom-info-window-with.html