2017-10-23 1 views
-1

최근에 "FusedLocationApi"가 사용되지 않으므로 Google 애플리케이션과 관련하여 내 앱을 수정 해 봅니다. 내 코드가 잘 작동하고, 내 활동이 위치를 얻지 만, 마커를 설정할 때 작동하지 않습니다. 나는 너무 혼란 스럽다. 내가 어디에서 실종되었는지를 알 수있는 누군가?Google지도 마커가 작동하지 않습니다.

여기 내 코드.

class DealerMapsActivity : FragmentActivity(), 
    OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener{ 

private val TAG = "DealerMapsActivity" 
private var mMap: GoogleMap? = null 
private var mGoogleApi: GoogleApiClient? = null 
private lateinit var binding: ActivityDealerMapsBinding 
private var myLastLocation: Location? = null 
private var myMarker: Marker? = null 
private var mFusedLocationClient: FusedLocationProviderClient? = null 

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    Log.d(TAG,"onCreate") 
    binding = DataBindingUtil.setContentView(this,R.layout.activity_dealer_maps) 
    val mapFragment = supportFragmentManager 
      .findFragmentById(R.id.map) as SupportMapFragment 
    mapFragment.getMapAsync(this) 
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this) 
} 

이 경우 위치를 얻으려면 LocationCallback을 사용합니다.

private var mLocationCallback: LocationCallback = object : LocationCallback() { 
    override fun onLocationResult(locationResult: LocationResult?) { 

     for (location in locationResult!!.locations) { 
      Log.i(TAG, "accuracy: "+ location.accuracy + " Location: " + 
        location.latitude + " " + location.longitude) 
      myLastLocation = location 
     } 
     SetMapMarker() 
     if(myLastLocation!!.accuracy > 0) { 
      Log.i(TAG, "set accuracy into text: "+ myLastLocation!!.accuracy) 
      binding.vAccurate.text = 
        "Accurate to "+myLastLocation!!.accuracy.toInt().toString()+ " meters" 
     } 
    } 

} 

여기 내지도 부호를 설정하는 코드입니다.

fun SetMapMarker(){ 
    Log.i(TAG, "set marker") 
    if(myMarker != null){ 
     myMarker!!.remove() 
    } 
    val latlng = LatLng(myLastLocation!!.latitude, myLastLocation!!.longitude) 
    if(myMarker == null){ 
     myMarker = mMap!!.addMarker(
       MarkerOptions() 
         .position(latlng) 
         .icon(BitmapDescriptorFactory 
           .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) 
     ) 
    } 
    myMarker!!.snippet = "ok" 
    myMarker!!.title = "i'm here" 

    val camera = CameraPosition 
      .builder() 
      .target(latlng) 
      .zoom(16.0F) 
      .build() 
    mMap!!.animateCamera(CameraUpdateFactory.newCameraPosition(camera)) 
} 

} @J 라 메쉬와 같은

+0

myMarker를 확인 했습니까 !! remove() 문이 myMarker를 null로 설정하고 있습니까? 아니오이면 null을 설정해야합니다. –

+0

나중에 내 마커를 제거한 후에 마커 위치를 업데이트하고 싶기 때문에 내 마커를 제거합니다. mymarker == null 인 경우 내 마커를 다시 설정 한 후 – Frendi

+0

컨트롤이 내부에 있는지 확인하십시오 (myMarker == null) { 조건? –

답변

0

은 매우 재미 있고, 좋습니다. 나는 이것으로 대체한다.

fun SetMapMarker(){ 
val latlng = LatLng(myLastLocation!!.latitude, myLastLocation!!.longitude) 
Log.i(TAG, "set marker") 
if(myMarker != null){ 
    myMarker!!.position = latlng 
} 

if(myMarker == null){ 
    myMarker = mMap!!.addMarker(
      MarkerOptions() 
        .position(latlng) 
        .icon(BitmapDescriptorFactory 
          .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) 
    ) 


// you can set title and message only one time not required all time (if you want to change message every time then write these line outside if statement) 
myMarker!!.snippet = "ok" 
myMarker!!.title = "i'm here" 
} 

val camera = CameraPosition 
     .builder() 
     .target(latlng) 
     .zoom(16.0F) 
     .build() 
mMap!!.animateCamera(CameraUpdateFactory.newCameraPosition(camera)) 
} 

희망을 돕는 사람.

관련 문제