Aligning the text in Google Maps Marker snippet - android

I want the string in my snippet to be aligned to the center.
Also, line breakers (\n) in the snippet are converted to spaces, is there a way to insert line breakers?
My relevant code:
GoogleMap map = ...
map.addMarker(new MarkerOptions().position(pos)
.title("title")
.snippet("body \n and mind");
Thanks

Using some examples, as well as answers from: custom info window adapter with custom data in map v2, I've come across the following solution:
marker.xml
<?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"
android:orientation="vertical" >
<TextView
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</RelativeLayout>
Java:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.marker, null);
TextView info= (TextView) v.findViewById(R.id.info);
info.setText(marker.getPosition().toString());
return v;
}
});

Related

Create a custom shape of title bar of marker in maps in android

I am using the following code, it gives a default rectangular shape.I want to change the shape of the title bar, as in the image
CameraPosition cameraPos = new CameraPosition.Builder()
.target(ll)
.zoom(18).bearing(300).tilt(45).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos), null);
if(racecourseroadmarker!=null)
racecourseroadmarker.remove();
racecourseroadmarker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(ll.latitude, ll
.longitude)).anchor(0.5f, 0.5f).draggable(true)
.title(getIntent().getStringExtra("busno")).snippet(location)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.bus)));
onMarkerDragStart(racecourseroadmarker);
onMarkerDrag(racecourseroadmarker);
racecourseroadmarker.showInfoWindow();
Try with this code it will help you to create custom view at google map marker snipt or title inside you can declare
View ll;
ll = getActivity().getLayoutInflater().inflate(R.layout.markerpopup, null);
tviNamePopup = (TextView) ll.findViewById(R.id.tviNamePopup);
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker markerss) {
// TODO Auto-generated method stub
String title = markerss.getTitle();
tviNamePopup.setText(title);
return null;
}
#Override
public View getInfoContents(Marker markerss) {
// TODO Auto-generated method stub
// do something here
return ll;
}
});
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLngs));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(2));
layout is : markerpopup.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:background="#drawable/editstyle"
android:orientation="vertical" >
<TextView
android:id="#+id/tviNamePopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textColor="#0000FF" />
</LinearLayout>

Center fixed marker in android v2 mapview

I want to set fixed marker in the center point of mapview while dragging map.It has been done at android Map V1 google mapview. But now it's deprecated.Now my question is, is it possible in android Map V2 google mapview ?(I have tried.but map doesn't show)
So you want something like a cross-hairs on the center of map, right?
I have not used MapView. But I have used Map Fragment, and the way i implement a fix marker on the map is use a ImageView, So you will end up with something like below:
<?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" >
<fragment
android:id="#+id/fragment1"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/cross_hairs" />
</RelativeLayout>
You could swap the MapFragment with MapView.
Placing the marker on the map will be inefficient, since you will need to keep updating the marker when the user pans the map.
Hope it helps!
I'm betting you used getMapCenter() which, as per Google Maps for Android v2, no longer available to use. But no worries, just use this:
GoogleMap.getCameraPosition().target
It will return a LatLng object which basically represents the center of the map. You can then use it to reposition the marker to the center every time there's a drag event by assigning an OnCameraChangedListener to your GoogleMap.
yourGMapInstance.setOnCameraChangeListener(new OnCameraChangedListener() {
#Override
public void onCameraChange (CameraPosition position) {
// Get the center of the Map.
LatLng centerOfMap = yourGMapInstance.getCameraPosition().target;
// Update your Marker's position to the center of the Map.
yourMarkerInstance.setPosition(centerOfMap);
}
});
There. I hope this helped!
Notice that setOnCameraChangeListener() has been deprecated, so you can use the new camera listeners (OnCameraIdleListener, OnCameraMoveListener, OnCameraMoveStartedListener, OnCameraMoveCanceledListener), for example:
map.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
#Override
public void onCameraMove() {
// Get the center of the map
LatLng center = map.getCameraPosition().target;
......
}
});
for more information, read this: https://developers.google.com/maps/documentation/android-api/events
Simply goto the XML File: Add Image View in map fragment and set the gravity to center. Then goto the Java File and add the following java code into your onMayReady Function:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linearLayout2" />
<ImageView
android:id="#+id/imageView_map_marker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="17dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:layout_gravity="center"
android:tooltipText="Select Location"
app:srcCompat="#drawable/ic_baseline_location_red" />
mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
#Override
public void onCameraMove() {
mMap.clear();
binding.imageViewMapMarker.setVisibility(View.VISIBLE);
}
});
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
#Override
public void onCameraIdle() {
binding.imageViewMapMarker.setVisibility(View.GONE);
selectedLatitude=mMap.getCameraPosition().target.latitude;
selectedLongitude=mMap.getCameraPosition().target.longitude;
MarkerOptions marker = new MarkerOptions().position(mMap.getCameraPosition().target).title("")
.icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_baseline_location_red));
mMap.addMarker(marker);
}
});

Zoom not working in a map inside scrollview in android

What i am doing::
I am using mapquest widget and api for maps
I am haveing the mapwidget inside the scrollview
I am able to click the map markers in the map also
What is happening::
I am not able to to zoom the map,
I can see zoom buttons clickit also but it wont zoom
code::
MyFragment.java
public class MyFragment extends Fragment{
protected MapView map;
AnnotationView annotation;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container,false);
map = (MapView) rootView.findViewById(R.id.map);
map.getController().setZoom(10);
map.getController().setCenter(new GeoPoint(12.918609,77.668912));
map.setBuiltInZoomControls(true);
// initialize the annotation to be shown later
annotation = new AnnotationView(map);
//addPolyOverlay();
// addLineOverlay();
return rootView;
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
addPoiOverlay();
}
// add an itemized overlay to map
private void addPoiOverlay() {
// use a custom POI marker by referencing the bitmap file directly,
// using the filename as the resource ID
Drawable icon = getResources().getDrawable(R.drawable.distance_icon_large);
final DefaultItemizedOverlay poiOverlay = new DefaultItemizedOverlay(icon);
// set GeoPoints and title/snippet to be used in the annotation view
OverlayItem poi1 = new OverlayItem(new GeoPoint (12.918609,77.668912), "Denver, Colorado", "MapQuest Headquarters");
poiOverlay.addItem(poi1);
OverlayItem poi2 = new OverlayItem(new GeoPoint (12.956868,77.701148), "Palo Alto, California", "AOL Offices");
poiOverlay.addItem(poi2);
// add a tap listener for the POI overlay
poiOverlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
#Override
public void onTap(GeoPoint pt, MapView mapView) {
// when tapped, show the annotation for the overlayItem
int lastTouchedIndex = poiOverlay.getLastFocusedIndex();
if(lastTouchedIndex>-1){
OverlayItem tapped = poiOverlay.getItem(lastTouchedIndex);
annotation.showAnnotationView(tapped);
}
}
});
map.getOverlays().add(poiOverlay);
}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mapquest.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="700dp"
android:apiKey="My-Key"
android:focusableInTouchMode="true"
android:clickable="true"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
You may need to override the touchlistener in the scrollview and then forward those events to the mapview if appropriate. The problem is that the scrollview is taking over the touch listener without properly forwarding it out.
mapview inside of a scrollview seems messy though....

Show title and snippet upon marker creation

I wish to add a marker to a googleMap with an editable title and snippet. The marker should appear when the user does a long click on the map and then the marker should appear showing a title of something like "click to edit".
The code is all up and running except that I can not figure out how to get the title to appear the moment the marker is created. I can only make it appear with a subsequent click on the marker. I can not see anything within MarkerOptions that allows me to do this. Did I miss something?
The option your looking for is not in MarkerOptions it is a function of the Marker itself. Here's a link to the related docs
marker.showInfoWindow();
To call this method you need to have the marker, or a reference to it. If you're doing this on creation it should be easy. Otherwise just store your makers in some collection - like a HashMap so they are easy to find - and you can display the info window whenever you want.
In case you want to display your marker's title and snippet in your own custom way - you may set it in the onMapReadyCallback.
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.marker_info, null);
TextView textViewTitle = (TextView) view.findViewById(R.id.marker_title);
textViewTitle.setText(marker.getTitle());
TextView textViewSnippet = (TextView) view.findViewById(R.id.marker_snippet);
textViewSnippet.setText(marker.getSnippet());
return view;
}
});
And here is my layout file for the same.
marker_info.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">
<TextView
android:id="#+id/marker_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="#+id/marker_snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>

How to offset the balloon view in a marker in Google Map V2?

When I click a marker, appears a balloon. But there are too space between the marker and the balloon, so how I can reduce this distance?. It's like using the setBalloonBottomOffset method in the V1 Google Map.
My custom balloon is this class:
public class CustomInfoWindowAdapter implements InfoWindowAdapter {
private final View mWindow;
private final View mContents;
public CustomInfoWindowAdapter(Activity activity) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mWindow = inflater.inflate(R.layout.ballon, null);
mContents = inflater.inflate(R.layout.ballon, null);
}
#Override
public View getInfoWindow(Marker marker) {
render(marker, mWindow);
return mWindow;
}
#Override
public View getInfoContents(Marker marker) {
render(marker, mContents);
return mContents;
}
private void render(Marker marker, View view) {
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.txtTitle));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.txtPlace));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
}
}
I show a marker like
marker.showInfoWindow();
My balloon xml is
<RelativeLayout
android:layout_width="250dp"
android:layout_height="75dp"
android:background="#drawable/ballon" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTitle"
style="#style/Ballon_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingLeft="15dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:singleLine="true"
android:text="Con mis amigos amigos" />
<TextView
android:id="#+id/txtPlace"
style="#style/Ballon_Place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="20dp"
android:paddingLeft="15dp"
android:paddingRight="50dp"
android:singleLine="true"
android:text="Puerta del sol" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="12dp"
android:paddingTop="18dp"
android:src="#drawable/icon_arrow" />
</RelativeLayout>
Map Markers are 'anchored' by default to the middle of the bottom of your layout (i.e., anchor(0.5,1)).
You can change the anchor point by using MarkerOptions.anchor when you create your Marker.
Have you tried using negative bottom margin for your whole balloon layout? Like so:
<RelativeLayout
android:layout_width="250dp"
android:layout_height="75dp"
android:background="#drawable/ballon"
android:layout_marginBottom="-8dp" >
Probably won't work or cause content to be cut off at the bottom, but still worth trying as the simplest approach. Perhaps there's another way to trick GoogleMap class but it's hard to figure out not having the source code to the API.
Another tip: you probably don't need your getInfoContents() method – just return null there. This method will only be called if you return null from getInfoWindow() (see here) – to incorporate your content view into default info window, which is clearly not your goal since you want to move balloon around.
Consequently, you can also throw out mContents, it's totally redundant, just takes up mem for another copy of your inflated view hierarchy that never gets used.
Tell me if the margin thing works for you. We could probably also try to make use of something like ((View)mWindow.getParent()) later when the item is being displayed but that would be trickier so I sugget to just try the margin first.
This is an old question, the current API has a MarkerOptions.infoWindowAnchor method which may help.

Categories

Resources