我是一个非常新手的程序员。我正在使用处理程序将服务中的位置数据获取到我的主要活动中,但是我想使用服务中的代码来更新地图。
GPS坐标已通过处理程序正确发送,但是我似乎对如何使用处理程序通过更新地图上的地图/图标来实际呈现数据表示怀疑。
地图始终将标记置于0.0,也不会将地图放置在通过处理程序发送的新位置。
//get GPS latitude and Longitude from service here
private void displayLatLong() {
final TextView latitudeView = (TextView) findViewById(R.id.latitude);
final TextView longitudeView = (TextView) findViewById(R.id.longitude);
final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
double latitude = 0.0;
double longitude = 0.0;
//latitudeCoords = "not empty"; //debug
//longitudeCoords = "not empty"; //debug
if(bound && locationService != null) {
latitude = locationService.getLastLatitude();
longitude = locationService.getlastLongitude();
}
String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
latitudeView.setText(latitudeStr);
longitudeView.setText(longitutdeStr);
latCoords = latitude;
longCoords = longitude;
// longitudeCoordsView.setText(longitudeCoords); //debug
// latitudeCoordsView.setText(longitudeCoords); //debug
handler.postDelayed(this, 1000);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Bind location service here so it keeps running even if activity is stopped.
Intent intent = new Intent(this, LocationService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
displayDistance();
displayLatLong();
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
LatLng player= new LatLng(latCoords, longCoords);
mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
}locat
获得位置后,您只需要更新地图即可。
[添加将更新地图的方法:
private void updateMapLocation() {
LatLng player= new LatLng(latCoords, longCoords);
mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
}
一旦填充了latCoords
和longCoords
成员变量,然后调用此方法:
//get GPS latitude and Longitude from service here
private void displayLatLong() {
final TextView latitudeView = (TextView) findViewById(R.id.latitude);
final TextView longitudeView = (TextView) findViewById(R.id.longitude);
final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
double latitude = 0.0;
double longitude = 0.0;
if(bound && locationService != null) {
latitude = locationService.getLastLatitude();
longitude = locationService.getlastLongitude();
}
String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
latitudeView.setText(latitudeStr);
longitudeView.setText(longitutdeStr);
latCoords = latitude;
longCoords = longitude;
// Add call to update map with location:
if (latCoords > 0 && longCoords > 0) {
updateMapLocation()
}
handler.postDelayed(this, 1000);
}
});
}