Adding Markers To Mapview

Nyptop

Active Member
Licensed User
Longtime User
Hi all,

I have had some excellent help for the JSON, but there is one last problem. Everything works fine with the JSON but the Mapview does not add the icons.

Here is the code:
B4X:
Sub JSONResponse_StreamFinish (Success As Boolean, TaskId As Int)
   MapView1.RemoveOverlay(MarkersOverlay1)
    Dim Icon As BitmapDrawable
    Icon.Initialize(LoadBitmap(File.DirAssets, "markerting.png"))
   Dim a As Int
   a = 0
   Dim List1 As List
   Dim JSON As JSONParser
   JSON.Initialize(File.ReadString(File.DirInternalCache, jsonfile &".json"))
   List1.Initialize
   List1 = JSON.NextArray
   Dim m As Map 'helper map for navigating
   m = List1.Get(0)
   Dim loc As Map
   Dim Markers As List
   Markers.Initialize
   loc = m.Get("location")
    For i = 0 To m.Size - 1
    Dim Marker1 As Marker
    Marker1.Initialize("", "", loc.Get ("longitude"), loc.Get ("latitude"),  Icon)
    Markers.Add(Marker1)
   a = a + 1
    Next
   MarkersOverlay1.AddMarkers(Markers)
   MapView1.AddOverlay(MarkersOverlay1)
   If a > 0 Then
   Else If a = 0 Then
   ToastMessageShow("Lucky you! There is no crime in your area!", True)
   Else
   End If
   HttpTimer.Enabled = False
   jsonfile = Rnd(0, 9999999)
End Sub

I have tried using the default marker using Null, but it is to no avail. In case there is a problem embeded deeper in the code then I have also attached the rest.

Thanks for all the help so far,

Neil
 

Attachments

  • avoidacrime.zip
    207.9 KB · Views: 443

Nyptop

Active Member
Licensed User
Longtime User
So something along the lines of this:

B4X:
Sub JSONResponse_StreamFinish (Success As Boolean, TaskId As Int)
   Dim MarkersOverlay1 As MarkersOverlay
   MarkersOverlay1.Initialize(MapView1, "MarkersOverlay1")
   MapView1.AddOverlay(MarkersOverlay1)
   MapView1.RemoveOverlay(MarkersOverlay1)
    Dim Icon As BitmapDrawable
    Icon.Initialize(LoadBitmap(File.DirAssets, "markerting.png"))
   Dim a As Int
   a = 0
   Dim List1 As List
   Dim JSON As JSONParser
   JSON.Initialize(File.ReadString(File.DirInternalCache, jsonfile &".json"))
   List1.Initialize
   List1 = JSON.NextArray
   Dim m As Map 'helper map for navigating
   m = List1.Get(0)
   Dim loc As Map
   Dim Markers As List
   Markers.Initialize
   loc = m.Get("location")
    For i = 0 To m.Size - 1
    Dim Marker1 As Marker
    Marker1.Initialize("", "", loc.Get ("longitude"), loc.Get ("latitude"),  Icon)
    Markers.Add(Marker1)
   a = a + 1
    Next
   MarkersOverlay1.AddMarkers(Markers)
   MapView1.AddOverlay(MarkersOverlay1)
   If a > 0 Then
   Else If a = 0 Then
   ToastMessageShow("Lucky you! There is no crime in your area!", True)
   Else
   End If
   HttpTimer.Enabled = False
   jsonfile = Rnd(0, 9999999)
End Sub

With the code above it still does not add the markers, though. What would you suggest I do?

Thanks for the help,

Neil
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your code
B4X:
  Dim MarkersOverlay1 As MarkersOverlay
  MarkersOverlay1.Initialize(MapView1, "MarkersOverlay1")
  MapView1.AddOverlay(MarkersOverlay1)
  MapView1.RemoveOverlay(MarkersOverlay1)
You Dim and add MarkersOverlay1 and just after you remove it !?
Try the code below
B4X:
  If MarkersOverlay1.IsInitialized Then
    MapView1.RemoveOverlay(MarkersOverlay1)
  End If
  Dim MarkersOverlay1 As MarkersOverlay
  MarkersOverlay1.Initialize(MapView1, "MarkersOverlay1")
  MapView1.AddOverlay(MarkersOverlay1)

Best regards.
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks for the help Klaus,

The mapview is still not loading though. My project as it stands is attached.

Cheers,

Neil
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

You have your latitude and longitude the wrong way around, it should be:

B4X:
Marker1.Initialize("", "", loc.Get ("latitude"), loc.Get ("longitude"), Icon)

Then the code tries to iterate through the JSONObject instead of the JSONArray when adding the Markers.

Try the attached code, it works a bit then crashes:

java.io.FileNotFoundException: /data/data/avoid.a.crime/cache/6370454.json (No such file or directory)

That happens after the map has requested crimes more than a few times - to start with it successfully gets crimes and adds them to the map.
Around 280 crimes show for where i live.

Martin.
 

Attachments

  • avoidacrime_warwound.zip
    208 KB · Views: 510
Upvote 0
Top