开发者

How do I show a map legend on an Android Google Map?

The web version of Google maps allows the developer to show a map legend that 开发者_如何学Cindicates the map's scale.

Is there any way to do this using the Android API?

Additional info:

  • I want to show a graphical "this distance is equal to X meters" kind of display like you get if you go to a google map.
  • It should be possible to use: getProjection() but I was just hoping there was a way to just enable the legend.
  • If you open the Google Maps app and select "Labs" you can enable the "Scale bar" that's what I want.


Have a look at how they did for the very similar Osmdroid which is for OpenStreetMap

ScaleBarOverlay

it should give some ideas. It's probably more fully featured than you need.


Create a layout with the text view, load it in your onCreate and then write the map scale into it using setText(). An example layout is below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:id="@+id/root">
        <com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                                 android:clickable="true"
                                 android:id="@+id/map"
                 android:apiKey="<YOUR KEY>"
                 android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentL
eft="true" android:layout_alignParentBottom="true"/>
                <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginLeft="10sp" android:layout_marginRight="10sp" android:layout_marginTop="10sp" android:id="@+id/scale" android:gravity="center_horizontal"></TextView>
</RelativeLayout>

In your activity, you can do

((TextView)findViewById(R.id.scale)).setText("<map scale>");

and it should appear on top of the map. You can style the text view however you want as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜