怎样的android移动地图下一个标记标记、地图、android

2023-09-06 00:25:01 作者:微笑ヽ诠释①切

喜在第一个问题是奇怪,但让我解释.. 假设我们有在地图上标记为中心,当我们与我们的手指在图上我们可以看到,当你离开了该地区的界限,使标记将不再显示。那么怎样才能做一个标记,将永远留在地图的中心,虽然我们移动地图的任何地方?有一个名为UBER一个应用程序,他们提供了这个功能,你不拖放您刚才移动地图标记和标记保持它的地位。

下面是一个出手

你可以看到绿色标记出现在屏幕上的中心,当我们把它保持它的地方的地图,展示了新的位置,当用户停止移动地图。我们怎么可以这样做?

我有一个code,显示的地址,当你拖动和放大器;下降的标志,如何再次显示该地址,但这次不是通过拖动,移动地图?希望你能理解任何想法将是巨大的。

 公共类MainActivity扩展AbstractMapActivity工具
    OnNavigationListener,OnInfoWindowClickListener,
    OnMarkerDragListener {
私有静态最后弦乐STATE_NAV =导航;
私有静态最终诠释[] MAP_TYPE_NAMES = {R.string.normal,
    R.string.hybrid,R.string.satellite,R.string.terrain};
私有静态最终诠释[] MAP_TYPES = {GoogleMap.MAP_TYPE_NORMAL,
    GoogleMap.MAP_TYPE_HYBRID,GoogleMap.MAP_TYPE_SATELLITE,
    GoogleMap.MAP_TYPE_TERRAIN};
私人GoogleMap的地图= NULL;
字符串filterAddress =;
标记标记;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);





    如果(readyToGo()){
        的setContentView(R.layout.activity_main);

        SupportMapFragment mapFrag =
            (SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。

        mapFrag.setRetainInstance(真正的);
        initListNav();

        地图= mapFrag.getMap();

        如果(savedInstanceState == NULL){
            CameraUpdate中心=
                CameraUpdateFactory.newLatLng(新经纬度(
                        41.003739,
                        28.999572));
            CameraUpdate变焦= CameraUpdateFactory.zoomTo(10);

            map.moveCamera(中心);
            map.animateCamera(变焦);

            addMarker(地图,41.003739,28.999572,R.string.un,R.string.united_nations);

              map.setInfoWindowAdapter(新PopupAdapter(getLayoutInflater()));
              map.setOnInfoWindowClickListener(本);
              map.setOnMarkerDragListener(本);




        }



    }
}

@覆盖
公共布尔onNavigationItemSelected(INT itemPosition,长的itemId){
    map.setMapType(MAP_TYPES [itemPosition]);

    返回(真);
}

@覆盖
公共无效的onSaveInstanceState(包savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(STATE_NAV,
            。getSupportActionBar()getSelectedNavigationIndex());
}

@覆盖
公共无效onRestoreInstanceState(包savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);

    getSupportActionBar()setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV))。
}

@覆盖
公共无效onInfoWindowClick(标记标记){
    Toast.makeText(这一点,marker.getTitle(),Toast.LENGTH_LONG).show();
}




@覆盖
公共无效onMarkerDragStart(标记标记){
    经纬度位置= marker.getPosition();



    。Log.d(的getClass()getSimpleName()的String.Format(由%F拖动:%F,
            position.latitude,
            position.longitude));
}

@覆盖
公共无效onMarkerDrag(标记标记){
    经纬度位置= marker.getPosition();


    Log.d(的getClass()。getSimpleName(),
            的String.Format(拖动到%F:%F,position.latitude,
                    position.longitude));
}

@覆盖
公共无效onMarkerDragEnd(标记标记){
    经纬度位置= marker.getPosition();


    字符串filterAddress =;
    地理codeR地理codeR =新的地缘codeR(
            getBaseContext(),Locale.getDefault());
    尝试 {
        名单<地址>地址=地理coder.getFromLocation(
                position.latitude,
                position.longitude,1);

        如果(addresses.size()大于0){
            为(中间体索引= 0;
            指数< addresses.get(0).getMaxAddressLineIndex();指数++)
                filterAddress + = addresses.get(0).getAddressLine(指数)+;
        }
    }赶上(IOException异常前){
        ex.printStackTrace();
    }赶上(例外E2){
        // TODO:处理异常

        e2.printStackTrace();
    }
    Log.d(的getClass()。getSimpleName(),
            的String.Format(拖动到%F:%F,position.latitude,
                    position.longitude));
    TextView的myTextView =(TextView中)findViewById(R.id.test);

    myTextView.setText(地址是:+ filterAddress);

}





私人无效initListNav(){
    ArrayList的<字符串>项目=新的ArrayList<字符串>();
    ArrayAdapter<字符串> NAV = NULL;
    动作条酒吧= getSupportActionBar();

    对于(整型:MAP_TYPE_NAMES){
        items.add(的getString(类型));
    }

    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
        NAV =
            新的ArrayAdapter<字符串>(
                    bar.getThemedContext(),
                    android.R.layout.simple_spinner_item,
                    项);
    }
    其他 {
        NAV =
            新的ArrayAdapter<字符串>(
                    本,
                    android.R.layout.simple_spinner_item,
                    项);
    }

    nav.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(NAV,这一点);
}

私人无效addMarker(GoogleMap的地图,双纬度,双经度,
        INT标题,INT片段)

{
    map.addMarker(新MarkerOptions()。位置(新的经纬度(纬度,经度))
            .icon(BitmapDesc​​riptorFactory.fromResource(R.drawable.pin))

            .draggable(真));
}

    }
 

下面是XML

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /布局1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_below =@ + ID / img_header>

<片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.google.android.gms.maps.SupportMapFragment/>

<的TextView
    机器人:ID =@ + ID /测试
    机器人:layout_width =match_parent
    机器人:layout_height =90dp
    机器人:layout_alignParentRight =真
    机器人:文本=地址
    机器人:文字颜色=#FF0000
    机器人:TEXTSIZE =22sp/>



< / RelativeLayout的>
 
受不了地图 App 里的各种广告 试试这款百度高德二合一的 Android 地图 Bmap

解决方案

我是不是能够获得尤伯杯的应用效果,但是我能够做同样的事情,也许它会做得很好的解决方案利用了GoogleMap.OnCameraChangeListener.

活动:

 公共类MyActivity扩展活动实现OnCameraChangeListener {

    ...

    @覆盖
    公共无效onCameraChange(CameraPosition位置){
        mMap.clear();
        mMap.addMarker(新MarkerOptions()
            .position(position.target)
            .title伪(position.toString())
        );
    }

    ...

}
 

的问题是,标记物不绘制在摄像机正在移动。不过,好像你已经创建了一个标记的覆盖。因此,与覆盖它应该像尤伯杯应用。

我希望这可以帮助,即使问题是一个几个月大,也许这将有利于未来的发展。

亲切的问候 三木

Hi at first question would be weird but let me explain.. Suppose that we have a centered marker on the map , when we move the map with our finger we can see that marker would no longer displayed when you got out of bounds of the area. So how can we make a marker that would remain always on the center of the map although we move the map anywhere ? There is an app named UBER and they provide this feature , you dont drag and drop the marker you just move the map and the marker remained it's position ..

here is a shot

as you can see green marker is on the center of screen when we move the map it remained its place but shows the new position when the user stop moving the map .. How can we do like this?

I have a code that shows the address when you drag&drop the marker, how about displaying the address again but this time not by dragging, moving the map? Hope you can understand any ideas would be great..

    public class MainActivity extends AbstractMapActivity implements
    OnNavigationListener, OnInfoWindowClickListener,
    OnMarkerDragListener {
private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
    R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
    GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
    GoogleMap.MAP_TYPE_TERRAIN };
private GoogleMap map=null;
String filterAddress = "";
Marker marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);





    if (readyToGo()) {
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFrag=
            (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        mapFrag.setRetainInstance(true);
        initListNav();

        map=mapFrag.getMap();

        if (savedInstanceState == null) {
            CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(
                        41.003739,
                        28.999572));
            CameraUpdate zoom=CameraUpdateFactory.zoomTo(10);

            map.moveCamera(center);
            map.animateCamera(zoom);

            addMarker(map, 41.003739, 28.999572, R.string.un, R.string.united_nations);

              map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
              map.setOnInfoWindowClickListener(this);
              map.setOnMarkerDragListener(this);




        }



    }
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    map.setMapType(MAP_TYPES[itemPosition]);

    return(true);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(STATE_NAV,
            getSupportActionBar().getSelectedNavigationIndex());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
}

@Override
public void onInfoWindowClick(Marker marker) {
    Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}




@Override
public void onMarkerDragStart(Marker marker) {
    LatLng position=marker.getPosition();



    Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
            position.latitude,
            position.longitude));
}

@Override
public void onMarkerDrag(Marker marker) {
    LatLng position=marker.getPosition();


    Log.d(getClass().getSimpleName(),
            String.format("Dragging to %f:%f", position.latitude,
                    position.longitude));
}

@Override
public void onMarkerDragEnd(Marker marker) {
    LatLng position=marker.getPosition();


    String filterAddress = "";
    Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocation(
                position.latitude, 
                position.longitude, 1);

        if (addresses.size() > 0) {
            for (int index = 0; 
            index < addresses.get(0).getMaxAddressLineIndex(); index++)
                filterAddress += addresses.get(0).getAddressLine(index) + " ";
        }
    }catch (IOException ex) {        
        ex.printStackTrace();
    }catch (Exception e2) {
        // TODO: handle exception

        e2.printStackTrace();
    } 
    Log.d(getClass().getSimpleName(),
            String.format("Dragging to %f:%f", position.latitude,
                    position.longitude));
    TextView myTextView = (TextView) findViewById(R.id.test);

    myTextView.setText("Address is: " + filterAddress);

}





private void initListNav() {
    ArrayList<String> items=new ArrayList<String>();
    ArrayAdapter<String> nav=null;
    ActionBar bar=getSupportActionBar();

    for (int type : MAP_TYPE_NAMES) {
        items.add(getString(type));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        nav=
            new ArrayAdapter<String>(
                    bar.getThemedContext(),
                    android.R.layout.simple_spinner_item,
                    items);
    }
    else {
        nav=
            new ArrayAdapter<String>(
                    this,
                    android.R.layout.simple_spinner_item,
                    items);
    }

    nav.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(nav, this);
}

private void addMarker(GoogleMap map, double lat, double lon,
        int title, int snippet)

{
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin))

            .draggable(true));
}

    }

Here is the xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/img_header" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

<TextView
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_alignParentRight="true"
    android:text="Address : "
    android:textColor="#FF0000"
    android:textSize="22sp" />



</RelativeLayout>

解决方案

I was not able to get the Uber app effect, however I was able to do something similar, perhaps it will do just fine for your solution making use of the GoogleMap.OnCameraChangeListener.

Activity:

public class MyActivity extends Activity implements OnCameraChangeListener {

    ...

    @Override
    public void onCameraChange(CameraPosition position) {
        mMap.clear();
        mMap.addMarker( new MarkerOptions()
            .position( position.target )
            .title( position.toString() )
        );
    }

    ...

}

The problem is, the marker is not drawn while the camera is being moved. However, it seems as though you have already created an overlay of a marker. So with an overlay it should work like the Uber application.

I hope this helps, even the question is a few months old, perhaps it will benefit future developers.

Kind regards Miki