我在这里读了很多帖子但是我无法解决我的问题。
我构建了一个简单的应用程序,显示了许多poi,信息视图,我能够绘制折线,并在地图中正确地设置行走方向,设置两个Lat Lng点。
现在我正在尝试添加START POINT作为用户当前位置的功能。如果我使用map.setMyLocationEnabled(true);它显示了我的位置的蓝点,我使用了“getMyLocation()。getLatitude();”和“getMyLocation()。getLongitude();”采取正确坐标的方法。但是当我运行我的应用程序时,它会因NullPoint错误而崩溃。
编辑:我解决了我的问题,我用解决方案编辑了MainAcrivity类
这是我的MainActivity.java
package com.directions.android;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.directions.route.Route;
import com.directions.route.Routing;
import com.directions.route.RoutingListener;
import com.google.android.gms.maps.CameraUpdate;
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.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
public class MyActivity extends FragmentActivity implements RoutingListener {
protected GoogleMap map;
protected LatLng start;
protected LatLng end;
protected Double myPosLat;
protected double myPosLng;
/**
* This activity loads a map and then displays the route and pushpins on it.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(47.3369217,9.353883));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
map.moveCamera(center);
map.animateCamera(zoom);
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
map.addMarker(new MarkerOptions().position(new LatLng(
location.getLatitude(), location.getLongitude())).title("You are here!"));
start = new LatLng(location.getLatitude(), location.getLongitude());
end = new LatLng(44.48861858, 11.36779726);
Routing routing = new Routing(Routing.TravelMode.WALKING);
routing.registerListener(MyActivity.this);
routing.execute(start, end);
}
});
}
@Override
public void onRoutingFailure() {
// The Routing request failed
}
@Override
public void onRoutingStart() {
// The Routing Request starts
}
@Override
public void onRoutingSuccess(PolylineOptions mPolyOptions, Route route) {
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(Color.RED);
polyOptions.width(8);
polyOptions.addAll(mPolyOptions.getPoints());
map.addPolyline(polyOptions);
// Start marker
MarkerOptions options = new MarkerOptions();
options.position(start);
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));
map.addMarker(options);
// End marker
options = new MarkerOptions();
options.position(end);
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green));
map.addMarker(options);
}
}
答案 0 :(得分:1)
我建议您将与map.getMyLocation()
相关的部分代码放在OnMyLocationChangeListener
中。获取MyLocation
需要花费时间在Android上。
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
myPosLat = location.getLatitude();
myPosLng = location.getLongitude();
start = new LatLng(myPosLat, myPosLng);
end = new LatLng(48.823515, 10.1246599);
Routing routing = new Routing(Routing.TravelMode.WALKING);
routing.registerListener(this);
routing.execute(start, end);
}
});
答案 1 :(得分:0)
我想出了我在问什么。这是将代码从当前位置添加到固定Lat Long的代码片段。
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
map.addMarker(new MarkerOptions().position(new LatLng(
location.getLatitude(), location.getLongitude())).title("You are here!"));
start = new LatLng(location.getLatitude(), location.getLongitude());
end = new LatLng(44.48861858, 11.36779726);
Routing routing = new Routing(Routing.TravelMode.WALKING);
routing.registerListener(MyActivity.this);
routing.execute(start, end);
}
});