如何更改谷歌地图标记上方的标题和片段设计

Posted

技术标签:

【中文标题】如何更改谷歌地图标记上方的标题和片段设计【英文标题】:How to change design of title and snippet above google maps marker 【发布时间】:2016-01-26 05:17:04 【问题描述】:

我目前正在开发一个涉及谷歌地图的应用程序。 我想更改位置标记上方信息的设计。但我无法弄清楚如何做到这一点。

解释一下;

我有这个:

我想要这个:

我将蓝色框的设计作为图像。但是我不知道我是否需要使用图像或编写代码使其看起来像图像。

这是我目前的代码:

MainActivity

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends FragmentActivity implements     LocationListener 

    GoogleMap map;
    LatLng myPosition;

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    isLocationEnabled(this);


    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    map = fm.getMap();

    // remove geolocation button
    map.getUiSettings().setMyLocationButtonEnabled(false);

    // Enabling MyLocation Layer of Google Map
    map.setMyLocationEnabled(true);

    // Getting LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) 
        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        myPosition = new LatLng(latitude, longitude);
        LatLng amsterdam = new LatLng(52.3667, 4.9000);


        // create marker
        MarkerOptions marker = new MarkerOptions().position(myPosition);
        // setting custom marker
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker));

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(myPosition);
        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker));
        markerOptions.title(" Title");

        markerOptions.snippet(" Here comes the address ");


        // create and add marker
        Marker locationMarker = map.addMarker(markerOptions);
        // always show info text
        locationMarker.showInfoWindow();
        // opening maps zoomed in at current location
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(amsterdam, 17.0f));



    


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) 
        return true;
    

    return super.onOptionsItemSelected(item);



@Override
public void onLocationChanged(Location location) 




public static boolean isLocationEnabled(Context context) 
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
        try 
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

         catch (Settings.SettingNotFoundException e) 
            e.printStackTrace();
        

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

     else 
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    


    

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv_location"
    android:layout_
    android:layout_ />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_
    android:layout_
    android:layout_below="@id/tv_location"
    class="com.google.android.gms.maps.SupportMapFragment"
    tools:layout="@layout/abc_action_bar_title_item" />


</RelativeLayout>

如果我使用了草率的编码或不良做法,请提前原谅。这些文件是我创建功能的游乐场,以便我以后可以将其放入实际的应用程序中。

【问题讨论】:

【参考方案1】:

要更改信息窗口的外观(“位置标记上方的信息”),请使用InfoWindowAdapter。在您的情况下,您想要替换整个窗口,包括内容和框架(带有指向标记的插入符号的东西)。因此,您的InfoWindowAdapter 将实现getInfoWindow() 并返回View 以用于给定的Marker。然后你在你的GoogleMap 上调用setInfoWindowAdapter(),告诉它在用户点击Marker 时向你的InfoWindowAdapter 询问View

This sample app 演示了这个概念,尽管我覆盖了getInfoContents() 而不是getInfoWindow(),因为我想替换内容但使用现有的信息窗口框架。你会得到这样的结果:

【讨论】:

以上是关于如何更改谷歌地图标记上方的标题和片段设计的主要内容,如果未能解决你的问题,请参考以下文章

带有标记的Android谷歌地图片段

如何在Android的谷歌地图片段中更改默认的蓝色圆形位置图标?

谷歌地图:在markerclusterer上方渲染标记

谷歌地图:如何更改标记的 z-index?

谷歌地图Android:圆圈上方的标记

在谷歌地图javascript api中 - 如何移动地图而不是标记