如何在Google Maps标记上方更改标题和摘要的设计 [英] How to change design of title and snippet above google maps marker

查看:81
本文介绍了如何在Google Maps标记上方更改标题和摘要的设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发涉及Google Maps的应用程序. 我想更改位置标记上方信息的设计.但是我不知道该怎么做.

i am currently working on an application involving google maps. I want to change the design of the information above the location marker. But i cannot figure out how to do this.

解释;

我有这个:

我想要这个:

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

I have the design for the blue box as an image. But i do not know if i need to use the image or write code to make it look like the image.

这是我到目前为止的代码:

Here is the code i have so far:

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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

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


</RelativeLayout>

如果我使用草率的编码或不良做法,请事先打扰.这些文件是我创建功能的场所,以便以后可以将其放入实际应用程序中.

Excuse me in advance if i used sloppy coding or bad practices. These files are a playground for me to create the functionality so that i can put it in the actual application later on.

推荐答案

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

To change the look of the info window ("the information above the location marker"), you use an InfoWindowAdapter. In your case, you want to replace the whole window, both the contents and the frame (the thing with the caret that points to the marker). So, your InfoWindowAdapter would implement getInfoWindow() and return the View to use for the given Marker. You then call setInfoWindowAdapter() on your GoogleMap to tell it to ask your InfoWindowAdapter for the View to use when the user taps on a Marker.

此示例应用演示了这个概念,尽管我覆盖getInfoContents()而不是getInfoWindow(),因为我想替换内容,但使用现有的信息窗口框架.您最终会遇到类似这样的情况:

This sample app demonstrates the concept, though I override getInfoContents() instead of getInfoWindow(), as I wanted to replace the contents but use the existing info window frame. You wind up with something like this:

这篇关于如何在Google Maps标记上方更改标题和摘要的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆