如何移动标记像汽车在Android地图上的道路上 [英] How to move marker like car goes on road in android map

查看:83
本文介绍了如何移动标记像汽车在Android地图上的道路上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。如何更改和修改然后获得解决方案。



私有静态最终LocationRequest REQUEST = LocationRequest.create()

.setInterval(5000)/ / 5秒

.setFastestInterval(16)// 16ms = 60fps

.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout。 my_location_demo);



id = getIntent()。getExtras()。getString(id);



mMessageView =(TextView)findViewById(R.id.message_text);

}



@Override

protected void onResume(){

super.onResume();

setUpMapIfNeeded();

setUpLocationClientIfNeeded();

mLocationClient.connect();

}



@Override

public void onPause(){ <无线电通信/>
super.onPause();

if(mLocationClient!= null){

mLocationClient.disconnect();

}

}



private void setUpMapIfNeeded(){

//进行空检查以确认我们还没有实例化地图。

if(mMap == null){

//尝试从SupportMapFragment获取地图。

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

.getMap();

//检查我们是否成功获得了地图。

if(mMap!= null){

// mMap.setMyLocationEnabled(true);

// mMap。

}

}



}



private void setUpLocationClientIfNeeded(){

if(mLocationClient == null){

mLocationClient = new LocationClient(

getApplicationContext(),

this,// C onnectionCallbacks

this); // OnConnectionFailedListener

}

}



/ **

*按钮获取当前位置。这演示了如何根据需要获取当前位置,

*而无需注册LocationListener。

* /

public void showMyLocation(View查看){

if(mLocationClient!= null && mLocationClient.isConnected()){

String msg =Location =+ mLocationClient.getLastLocation();

Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT)。show();

}

}



/ **

* {@link LocationListener}的实施。

* /

@Override

public void onLocationChanged(位置位置){

mMessageView.setText(Location =+ location);

if(location.getAccuracy()< ; 100.0 && location.getSpeed()< 6.95)

{

mMap.clear();

new Background()。execute( location.getLatitude()+,location.getLongitude()+);

m = mMap.addMarker(new MarkerOptions()。position(ne w LatLng(location.getLatitude(),location.getLongitude()))

.title(Driver)

.icon(BitmapDescriptorFactory.fromResource(R.drawable。出租车)));

mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude())));

// animateMarker (m,new LatLng(location.getLatitude(),location.getLongitude()),false);

}

}



/ **

*连接到GCore时调用回调。实现{@link ConnectionCallbacks}。

* /

@Override

public void onConnected(Bundle connectionHint){

mLocationClient.requestLocationUpdates(

REQUEST,

this); // LocationListener

}



/ **

*与GCore断开连接时调用回调。实施{@link ConnectionCallbacks}。

* /

@Override

public void onDisconnected(){

//什么都不做

}



/ **

* {@link OnConnectionFailedListener}的实现。< br $>
* /

@Override

public void onConnectionFailed(ConnectionResult result){

//什么都不做
}



}

This is my code. how to change and modify then get solution.

private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location_demo);

id = getIntent().getExtras().getString("id");

mMessageView = (TextView) findViewById(R.id.message_text);
}

@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
}

@Override
public void onPause() {
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// mMap.setMyLocationEnabled(true);
// mMap.
}
}

}

private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
getApplicationContext(),
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}

/**
* Button to get current Location. This demonstrates how to get the current Location as required,
* without needing to register a LocationListener.
*/
public void showMyLocation(View view) {
if (mLocationClient != null && mLocationClient.isConnected()) {
String msg = "Location = " + mLocationClient.getLastLocation();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}

/**
* Implementation of {@link LocationListener}.
*/
@Override
public void onLocationChanged(Location location) {
mMessageView.setText("Location = " + location);
if(location.getAccuracy() < 100.0 && location.getSpeed() < 6.95)
{
mMap.clear();
new Background().execute(location.getLatitude()+"",location.getLongitude()+"");
m = mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude()))
.title("Driver")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi)));
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
// animateMarker(m, new LatLng(location.getLatitude(), location.getLongitude()), false);
}
}

/**
* Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onConnected(Bundle connectionHint) {
mLocationClient.requestLocationUpdates(
REQUEST,
this); // LocationListener
}

/**
* Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onDisconnected() {
// Do nothing
}

/**
* Implementation of {@link OnConnectionFailedListener}.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}

}

推荐答案

首先,导入绘制线条所需的内容:



import com.google.android.gms.maps.model.Polyline;

import com.google.android.gms.maps .model.PolylineOptions;

为ArrayList和Polyline创建成员变量:



private ArrayList< latlng>点; //添加

折线; //添加

在onCreate()中初始化点数:



@Override

protected void onCreate(Bundle savedInstanceState ){$ / $
super.onCreate(savedInstanceState);



points = new ArrayList< latlng>(); //添加

// ...............

然后,在onLocationChanged()中,添加你得到的每个点ArrayList:



LatLng latLng = new LatLng(纬度,经度); //你已经有了这个



points.add(latLng); //添加



redrawLine(); //添加

从这个答案中,定义你的redrawLine()方法。

删除对addMarker()的所有其他调用,因为你将调用你的clear()地图,删除所有标记和折线。



private void redrawLine(){



googleMap.clear (); //清除所有标记和折线



PolylineOptions options = new PolylineOptions()。width(5).color(Color.BLUE).geodesic(true);

for(int i = 0; i< points.size(); i ++){

LatLng point = points.get(i);

options.add(point);

}

addMarker(); //在当前位置添加标记

line = googleMap.addPolyline(options); //添加折线

}
First, import what you need for drawing the lines:

import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
Create member variables for the ArrayList and the Polyline:

private ArrayList<latlng> points; //added
Polyline line; //added
Initialize points in onCreate():

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

points = new ArrayList<latlng>(); //added
//...............
Then, in onLocationChanged(), add each point you get to the ArrayList:

LatLng latLng = new LatLng(latitude, longitude); //you already have this

points.add(latLng); //added

redrawLine(); //added
Taking from this answer, define your redrawLine() method.
Remove all other calls to addMarker(), since you will be calling clear() on your map, which removes all Markers and Polylines.

private void redrawLine(){

googleMap.clear(); //clears all Markers and Polylines

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
addMarker(); //add Marker in current position
line = googleMap.addPolyline(options); //add Polyline
}


这篇关于如何移动标记像汽车在Android地图上的道路上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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