user1517638
user1517638

Reputation: 982

Dynamically increase the size of custom marker in google maps

In my google map application, I am using custom marker for different locations. I implemented this successfully but I need to dynamically increase the size of the custom marker. I used the following code

imgView.getLayoutParams().height = 200;
imgView.getLayoutParams().width = 200;

the codes are working fine in some devices but in nexus4 and 5 the marker is very big. please help me to fix the issue

Custom Marker XMl

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView     
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/marker" />

    <TextView           
        android:layout_width="36dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="14sp"
        android:textStyle="bold" />

</RelativeLayout>

Upvotes: 0

Views: 378

Answers (1)

Tim
Tim

Reputation: 43314

With

imgView.getLayoutParams().height = 200;

you are specifying the new height in pixels, ignoring the current device's pixel density. You should not do this.

You can use your current method to enlarge the images by doing e.g.

int height = imgView.getLayoutParams().height;
imgView.getLayoutParams().height = height * 2;

to make them 2x bigger. Instead of making the image a hardcoded 200px regardless of the device's pixel density, this will make the image 140px if it was 70px, or 200px if it was 100px.
This will nicely adapt itself to the current device.

You can also work with dp, by specifying a dp value and then converting it to pixels:

int dp = 72;
int height = imgView.getLayoutParams().height;
imgView.getLayoutParams().height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

This will set the height to 72 by translating 72 into the right amount of pixels for the current device.

Upvotes: 0

Related Questions