您好我需要帮助添加标记到谷歌地图以显示我当前的位置。即使在使用addMarker()方法并检查API密钥之后,标记仍然不可见。我能够获得纬度,经度值,但标记不可见。我正在尝试为片段做这件事。
这是我的完整代码:
public class WhereAmI extends Fragment {
GoogleMap googleMap;
TextView tv;
Location current;
LocationManager lm;
LocationListener locLis;
public static double lat=0.0000;
public static double longi=0.0000;
public static String addressString="Address Not Available";
LatLng Locateme;
public static String disp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.whereami, container,false);
tv=(TextView) v.findViewById(R.id.textView1);
lm=(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locLis=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
switch (arg1) {
case LocationProvider.AVAILABLE:
Toast.makeText(getActivity(), "AVAILABLE", Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(getActivity(), "OUT_OF_SERVICE", Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(getActivity(), "TEMPORARILY_UNAVAILABLE", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getActivity(), provider+" Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getActivity(), provider+" Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location loc) {
addressString="";
lat=loc.getLatitude();
longi=loc.getLongitude();
Geocoder gc=new Geocoder(getActivity());
List<Address> address;
try{
address=gc.getFromLocation(lat, longi, 1);
for(int i=0;i<address.size();i++)
{ Address a=address.get(i);
for(int j=0;j<a.getMaxAddressLineIndex();j++){
addressString+=a.getAddressLine(j)+"\n";
}}
disp="Latitude: "+lat+"\n"+"Longitude: "+longi+"\n"+"Address: "+addressString;
tv.setText(disp);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat,longi)).title("Current Location"));
marker.setIcon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(lat, longi)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}catch(Exception e){
e.printStackTrace();
}
}};
return v;
}
@Override
public void onResume() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, locLis);
super.onResume();
}
@Override
public void onPause() {
lm.removeUpdates(locLis);
super.onPause();
}
}
答案 0 :(得分:0)
因为您要将NULL
发送到纬度和经度等参数。
确保你获得指向点的坐标。