public class map2 extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private static final String TAG =null ;
private GoogleMap mGoogleMap;
private Geocoder mGeocoder;
String Startlongitude,Endlongitude;
String Startlattitude,Endlattitude;
double lon,Eat,Elot;
double lat;
LatLng latlon,latlon2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mSupportMapFragment.getMapAsync(this);
mGeocoder = new Geocoder(this, Locale.getDefault());
Bundle b = getIntent().getExtras();
String Array=b.getString("ITEM_EXTRA");
String Tripname = getIntent().getExtras().getString("Trip");
List<LatLng> routeArray = new ArrayList<>();
try {
JSONArray jO=new JSONArray(Array);
for(int i=0;i<jO.length();i++)
{
JSONObject tripObject=jO.getJSONObject(i);
String Trips=tripObject.getString("TripNO");
JSONObject msg=tripObject.getJSONObject("Trips");
if(Trips.equals(Tripname))
{
JSONArray msgObject=msg.getJSONArray("Trip1");
for(int j=0;j<msgObject.length();j++)
{
JSONObject d=msgObject.getJSONObject(j);
Startlongitude=d.getString("SfltLogitude");
Startlattitude=d.getString("Sfltlattitude");
Endlongitude = d.getString("EfltLogitude");
Endlattitude = d.getString("Efltlattitude");
Eat=Double.parseDouble(Endlattitude.trim());
Elot=Double.parseDouble(Endlongitude.trim());
lat=Double.parseDouble(Startlattitude.trim());
lon=Double.parseDouble(Startlongitude.trim());
latlon= new LatLng(lat, lon);
latlon2=new LatLng(Eat,Elot);
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(latlon,latlon2);
polyLineOptions.width(2);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);
break;
}
}
else{
String task=tripObject.getString("Trips");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.normal:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.satellite:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.terrain:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.hybrid:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.none:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
}
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(this);
}
@Override
public void onMyLocationChange(Location location) {
}
}
这里我从JsonServer获取LAttitude和经度,并将其转换为双打......但我无法在谷歌地图中绘制路径?? IT正在向我显示此错误“Polyline com.google。关于空对象引用的android.gms.maps.GoogleMap.addPolyline(com.google.android.gms.maps.model.PolylineOptions)'
答案 0 :(得分:0)
当地图尚未准备好时,您正在地图上设置折线。因此,请移动您在onMapReady()
方法中设置折线的代码。
喜欢:
public class map2 extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private static final String TAG =null ;
private GoogleMap mGoogleMap;
private Geocoder mGeocoder;
String Startlongitude,Endlongitude;
String Startlattitude,Endlattitude;
double lon,Eat,Elot;
double lat;
LatLng latlon,latlon2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mSupportMapFragment.getMapAsync(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.normal:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.satellite:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.terrain:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.hybrid:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.none:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
}
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(this);
mGeocoder = new Geocoder(this, Locale.getDefault());
Bundle b = getIntent().getExtras();
String Array=b.getString("ITEM_EXTRA");
String Tripname = getIntent().getExtras().getString("Trip");
List<LatLng> routeArray = new ArrayList<>();
try {
JSONArray jO=new JSONArray(Array);
for (int i = 0; i < jO.length(); i++)
{
JSONObject tripObject=jO.getJSONObject(i);
String Trips = tripObject.getString("TripNO");
JSONObject msg = tripObject.getJSONObject("Trips");
if (Trips.equals(Tripname))
{
JSONArray msgObject=msg.getJSONArray("Trip1");
for (int j = 0; j < msgObject.length(); j++)
{
JSONObject d = msgObject.getJSONObject(j);
Startlongitude = d.getString("SfltLogitude");
Startlattitude = d.getString("Sfltlattitude");
Endlongitude = d.getString("EfltLogitude");
Endlattitude = d.getString("Efltlattitude");
Eat=Double.parseDouble(Endlattitude.trim());
Elot=Double.parseDouble(Endlongitude.trim());
lat = Double.parseDouble(Startlattitude.trim());
lon = Double.parseDouble(Startlongitude.trim());
latlon = new LatLng(lat, lon);
latlon2 = new LatLng(Eat,Elot);
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(latlon,latlon2);
polyLineOptions.width(2);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);
break;
}
}
else {
String task = tripObject.getString("Trips");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMyLocationChange(Location location) {
}
}